最近模仿京东顶部搜索条效果制作的一个小demo,特贴到这里,今后如果有用到可以参考一下,代码如下

 #define kScreenWidth  [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height #import "mainViewController.h" @interface mainViewController () <UIScrollViewDelegate, UITextFieldDelegate> {
// 滚动列表
UIScrollView *infoListView;
// 滚动列表上展示信息的view
UIView *infoView;
// 顶部广告栏
UIView *topAdView;
// 定位按钮
UIButton *loacBtn;
// 搜索输入框
UITextField *searchBarTF;
// 二维码扫描按钮
UIButton *scanBtn;
// 顶部渐变背景条
UIView *bgView;
// 承载按钮,搜索输入框等控件的条
UIView *searchBarBgView; } @end @implementation mainViewController - (void)viewDidLoad {
[super viewDidLoad];
// 创建scrollView
[self createScrollview];
// 创建顶部广告栏
[self createTopAdView];
// 创建根据滚动渐变的顶部栏
[self createBgView];
// 创建导航条
[self createSearchBar]; } - (void)viewWillAppear:(BOOL)animated {
[self.navigationController setNavigationBarHidden:YES];
[super viewWillAppear:animated];
} #pragma mark - 创建scrollView
- (void)createScrollview {
infoListView = [[UIScrollView alloc] initWithFrame:CGRectMake(, , kScreenWidth, kScreenHeight)];
infoListView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:infoListView];
infoListView.contentSize = CGSizeMake(, );
infoListView.scrollEnabled = YES;
infoListView.delegate = self;
// infoListView.showsVerticalScrollIndicator = NO;
// 将infoView加载到scrollView上
infoView = [[UIView alloc] initWithFrame:CGRectMake(, , kScreenWidth, )];
infoView.userInteractionEnabled = YES;
infoView.backgroundColor = [UIColor lightGrayColor];
[infoListView addSubview:infoView]; }
#pragma mark - 创建顶部广告栏
- (void)createTopAdView {
topAdView = [[UIView alloc] initWithFrame:CGRectMake(, , kScreenWidth, )];
topAdView.backgroundColor = [UIColor blackColor];
[infoView addSubview:topAdView]; }
#pragma mark - 顶部渐变栏
- (void)createBgView {
bgView = [[UIView alloc] initWithFrame:CGRectMake(, , kScreenWidth, )];
bgView.backgroundColor = [UIColor orangeColor];
bgView.alpha = 0.0;
[self.view addSubview:bgView];
[self.view bringSubviewToFront:bgView];
}
#pragma mark - 搜索栏,地址定位按钮, 扫描按钮
- (void)createSearchBar {
// 1.创建导航条背景
searchBarBgView = [[UIView alloc] initWithFrame:CGRectMake(, , kScreenWidth, )];
searchBarBgView.backgroundColor = [UIColor clearColor];
[self.view addSubview:searchBarBgView];
// 2.创建label
UILabel *loacLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
loacLabel.text = @"我在";
loacLabel.textColor = [UIColor whiteColor];
loacLabel.textAlignment = NSTextAlignmentRight;
loacLabel.font = [UIFont systemFontOfSize:14.0];
loacLabel.backgroundColor = [UIColor clearColor];
[searchBarBgView addSubview:loacLabel];
// 3.创建定位按钮
loacBtn = [UIButton buttonWithType:UIButtonTypeCustom];
loacBtn.frame = CGRectMake(, , , );
loacBtn.backgroundColor = [UIColor orangeColor];
[loacBtn setImage:[UIImage imageNamed:@"1.8"] forState:UIControlStateNormal];
[loacBtn setImageEdgeInsets:UIEdgeInsetsMake(, , , )];
[loacBtn setTitle:@"深圳" forState:UIControlStateNormal];
[loacBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[loacBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
loacBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
loacBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
loacBtn.contentEdgeInsets = UIEdgeInsetsMake(, -, , );
loacBtn.backgroundColor = [UIColor clearColor];
[searchBarBgView addSubview:loacBtn];
// 4.搜索框
// 4.1创建搜索框背景
UIImageView *searchBgView = [[UIImageView alloc] initWithFrame:CGRectMake(, , , )];
[searchBgView setImage:[UIImage imageNamed:@"1.6"]];
searchBgView.userInteractionEnabled = YES;
[searchBarBgView addSubview:searchBgView];
// 4.2 搜索图标
UIImageView *searchIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.5"]];
searchIcon.frame = CGRectMake(, searchBgView.frame.size.height/ - /, , );
[searchBgView addSubview:searchIcon];
// 4.3 搜索输入框
searchBarTF = [[UITextField alloc] initWithFrame:CGRectMake(, searchBgView.frame.size.height/ - /, , )];
searchBarTF.backgroundColor = [UIColor clearColor];
searchBarTF.textColor = [UIColor blackColor];
searchBarTF.textAlignment = NSTextAlignmentLeft;
searchBarTF.font = [UIFont systemFontOfSize:15.0];
searchBarTF.delegate = self;
[searchBarTF setClearButtonMode:UITextFieldViewModeWhileEditing];
[searchBgView addSubview:searchBarTF];
// 5. 扫描二维码按钮
scanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
scanBtn.frame = CGRectMake(kScreenWidth - , , , );
[scanBtn setImage:[UIImage imageNamed:@"icon_scan_white"] forState:UIControlStateNormal];
[searchBarBgView addSubview:scanBtn]; [self.view bringSubviewToFront:searchBarBgView];
} #pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (infoListView.contentOffset.y >= ) { // 当滚动视图滚动到y值大于等于10的情况下
[UIView animateWithDuration:0.25 animations:^{
bgView.alpha = 0.8;
}];
} else if (infoListView.contentOffset.y < && infoListView.contentOffset.y >= -) {
[UIView animateWithDuration:0.25 animations:^{
bgView.alpha = 0.0;
bgView.hidden = NO;
searchBarBgView.hidden = NO;
}];
} else if (infoListView.contentOffset.y < -) {
[UIView animateWithDuration:0.25 animations:^{
bgView.hidden = YES;
searchBarBgView.hidden = YES;
}];
}
} #pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[searchBarTF resignFirstResponder];
return YES;
} @end

模仿京东顶部搜索条效果制作的一个小demo的更多相关文章

  1. css实现京东顶部导航条

    1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="U ...

  2. WPF制作的一个小功能,智能提示(IntelliSense)

    原文http://www.cnblogs.com/scheshan/archive/2012/06/30/2570867.html 最近WPF项目中遇到一个需求,需要给一个RichTextBox添加智 ...

  3. 一个小demo 实用selenium 抓取淘宝搜索页面内的产品内容

    废话少说,上代码 #conding:utf-8 import re from selenium import webdriver from selenium.webdriver.common.by i ...

  4. 基于Two.js实现的一个小demo,星球环绕动画效果

    下面是核心js code HTML就不贴了,需要引入two.js文件: var elem = document.getElementById('draw-animation'); var two = ...

  5. 环形文字 + css3制作图形 + animation无限正反旋转的一个小demo

    少啰嗦,先看效果图: (注意文字和太极图均可旋转,太极图使用css写成的!) css: /*太极图css--*/ .Taiji { margin: 100px; width: 192px; heigh ...

  6. ElasticSearch7.X.X-初见-模仿京东搜索的实战

    目录 简介 聊聊Doug Cutting ES&Solr&Lucene ES的安装 安装可视化界面ES head插件 了解ELK 安装Kibana ES核心概念 文档 类型 索引 倒排 ...

  7. 【jQuery】页面顶部显示的进度条效果

    <!Doctype html> <html> <head> <title>页面顶部显示的进度条效果</title> <meta htt ...

  8. uni-app自定义导航栏按钮|uniapp仿微信顶部导航条

    最近一直在学习uni-app开发,由于uniapp是基于vue.js技术开发的,只要你熟悉vue,基本上很快就能上手了. 在开发中发现uni-app原生导航栏也能实现一些顶部自定义按钮+搜索框,只需在 ...

  9. CSS 实现滚动进度条效果

    参考:https://www.w3cplus.com/css/pure-css-create-scroll-indicator.html 前言:细化总结.参考的文章作者已经写的很详细了.这里在从初学者 ...

随机推荐

  1. HDU 5328 Problem Killer

    题意:给一段序列,求连续的子序列中最长的等差数列或者等比数列的长度. 解法:O(n)的扫两遍一次判等差一次判等比就好了. 代码: #include<stdio.h> #include< ...

  2. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.3.1

    Let $A=A_1\oplus A_2$. Show that (1). $W(A)$ is the convex hull of $W(A_1)$ and $W(A_2)$; i.e., the ...

  3. Tomcat问题笔记

    1. Tomcat服务器只能同步WebContent目录到webapps下面,如果WebContent里面的.html文件引用了与WebContent文件夹同级目录下的一个.js文件,Tomcat服务 ...

  4. TCP服务端和客户端的框架

    提供一个框架服务器端: 创建一个Socket sFd=socket(AF_INET,SOCK_STREAM,0) 把Socket和本机的IP,TCP口绑定 bind(sFd,(structsockad ...

  5. 【转载】C内存对齐

    http://blog.csdn.net/hbuxiaofei/article/details/9491953 当你看到这个标题,仍想往下读的时候说明你已经开始关注数据在内存存储问题了. 好吧,下面先 ...

  6. algorithm@ Divide two integers without using multiplication, division and mod operator. (Bit Operation)

    #include<bits/stdc++.h> using namespace std; int divide(int dividend, int divisor) { long long ...

  7. SQL Server Cpu 100% 的常见原因及优化

    SQL Server Cpu 100% 的情况并不太常见,一般引起 SQL Server 产生性能问题的,都是 阻塞.连接数.IO 磁盘等.所以,一般SQL Server 的使用率都是比较低的.但是, ...

  8. 电脑突然死机,系统日志记录事件ID=6008

    刚才正在写代码,在一次保存之后,正要刷新看下效果,电脑突然关机,没有任何提示或延迟.我的笔记本电池是一直插上的,也连接着电源. 重新开机之后,找到系统日志查看.只有这一条错误记录:非正常关机,事件60 ...

  9. MFRCC522 SPI无法通讯【worldsing笔记】

    用单片机于MRFC522与单片接时,加上485通讯后出现很诡异的像: 只要485芯片上有收到外部发送的信号时RC522就死掉,经过仿真卡在了SPI的收发部分(等待回复) u8 MFRC522Write ...

  10. 高效使用Bitmaps(一) 大Bitmap的加载

    转载:http://my.oschina.net/rengwuxian/blog/182885 高效使用Bitmaps有什么好处? 我们常常提到的“Android程序优化”,通常指的是性能和内存的优化 ...