模仿京东顶部搜索条效果制作的一个小demo
最近模仿京东顶部搜索条效果制作的一个小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的更多相关文章
- css实现京东顶部导航条
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="U ...
- WPF制作的一个小功能,智能提示(IntelliSense)
原文http://www.cnblogs.com/scheshan/archive/2012/06/30/2570867.html 最近WPF项目中遇到一个需求,需要给一个RichTextBox添加智 ...
- 一个小demo 实用selenium 抓取淘宝搜索页面内的产品内容
废话少说,上代码 #conding:utf-8 import re from selenium import webdriver from selenium.webdriver.common.by i ...
- 基于Two.js实现的一个小demo,星球环绕动画效果
下面是核心js code HTML就不贴了,需要引入two.js文件: var elem = document.getElementById('draw-animation'); var two = ...
- 环形文字 + css3制作图形 + animation无限正反旋转的一个小demo
少啰嗦,先看效果图: (注意文字和太极图均可旋转,太极图使用css写成的!) css: /*太极图css--*/ .Taiji { margin: 100px; width: 192px; heigh ...
- ElasticSearch7.X.X-初见-模仿京东搜索的实战
目录 简介 聊聊Doug Cutting ES&Solr&Lucene ES的安装 安装可视化界面ES head插件 了解ELK 安装Kibana ES核心概念 文档 类型 索引 倒排 ...
- 【jQuery】页面顶部显示的进度条效果
<!Doctype html> <html> <head> <title>页面顶部显示的进度条效果</title> <meta htt ...
- uni-app自定义导航栏按钮|uniapp仿微信顶部导航条
最近一直在学习uni-app开发,由于uniapp是基于vue.js技术开发的,只要你熟悉vue,基本上很快就能上手了. 在开发中发现uni-app原生导航栏也能实现一些顶部自定义按钮+搜索框,只需在 ...
- CSS 实现滚动进度条效果
参考:https://www.w3cplus.com/css/pure-css-create-scroll-indicator.html 前言:细化总结.参考的文章作者已经写的很详细了.这里在从初学者 ...
随机推荐
- JS组件Bootstrap实现弹出框和提示框效果代码
这篇文章主要介绍了JS组件Bootstrap实现弹出框和提示框效果代码,对弹出框和提示框感兴趣的小伙伴们可以参考一下 前言:对于Web开发人员,弹出框和提示框的使用肯定不会陌生,比如常见的表格新增和编 ...
- log4j和web.xml配置webAppRootKey 的问题
在tomcat下部署两个或多个项目时,web.xml文件中最好定义webAppRootKey参数,如果不定义,将会缺省为“webapp.root”,如下: <!-- 应用路径 --> &l ...
- 【Python】Python-用大写字母打印你的名字
与孩子一起编程,有个作业题:用大写字母打印你的名字. CCCC A RRRRR C C A A R R C A A R R C AAAAAAA RRRRR C C A A R R CCCC A A R ...
- 2014 多校联合训练赛6 Fighting the Landlords
本场比赛的三个水题之一,题意是两个玩家每人都持有一手牌,问第一个玩家是否有一种出牌方法使得在第一回和对方无牌可出.直接模拟即可,注意一次出完的情况,一开始没主意,wa了一发. #include< ...
- 单元测试之获取Spring下所有Bean
单元测试中,针对接口的测试是必须的,但是如何非常方便的获取Spring注册的Bean呢? 如果可以获取所有的Bean,这样就可以将这个方法放到基类中,方便后面所有单元测试类的使用,具体实现如下: im ...
- 【原】Storm配置
Storm入门教程 1. Storm基础 Storm Storm主要特点 Storm基本概念 Storm调度器 Storm配置 Guaranteeing Message Processing(消息处理 ...
- 利用CryptoStream进行加密解密
public class DBSecurity { //sKey sIV这两个自己随意设定,不能外泄 private const string sKey = "11,22,33,43,34, ...
- JavaScript(class0526)
什么是JavaScript? HTML只是描述网页长相的标记语言,没有计算.判断能力,如果所有计算.判断(比如判断文本框是否为空.判断两次密码是否输入一致)都放到服务器端执行的话网页的话页面会非常慢. ...
- [原]Java面试题-输入一个整型数组,找出最大值、最小值,并交换。
[Date]2013-09-19 [Author]wintys (wintys@gmail.com) http://wintys.cnblogs.com [Content]: 1.面试题 输入一个整型 ...
- NOIP2010 引水入城
4引水入城 题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N 行M 列的矩形,如上图所示,其中每个格子都代表一座城市,每座城市都有一个 ...