模仿京东顶部搜索条效果制作的一个小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 前言:细化总结.参考的文章作者已经写的很详细了.这里在从初学者 ...
随机推荐
- Today I Cooked the Sun Yat-Sen University [2007-09-25 12:37:39]
Aha,yes,it is! And I paticipate in the school page of sysu...and cann't change my previous csu to sy ...
- 【Python】使用python的tornado配合html页面示例
背景:java写的非标加密算法,测试时执行java工程进行解密测试,很不方便. 目的:想写个web页面,使得任何测试人员都可以在输入加密串时得到解密后字段,方便日志查询及字段核对.(额,算法部分就不写 ...
- VTK三维重建(1)-使用VTK读取DICOM,并动态输出
[效果显示] 将脚部骨骼扫描的CT照片进行的连续读取, 运行结果存为了两个动态gif, 不知道能不能正常显示 [程序实现] int main(int argc, char* argv[]) { // ...
- VBScript: 正则表达式(RegExp对象)
RegExp对象是VBScript中用于提供简单地正则表达式支持的对象.VBScript中所有和正则表达式有关的属性和方法都有这个对象有关联. 一.RegExp对象的属性和方法(三个属性,三个方法) ...
- 【整理】C++虚函数及其继承、虚继承类大小
参考文章: http://blog.chinaunix.net/uid-25132162-id-1564955.html http://blog.csdn.net/haoel/article/deta ...
- 稀疏表示(sparse representation)和字典学习
近十几年来,稀疏(sparsity)已经成为信号处理及其应用领域中处于第一位的概念之一.近来,研究人员又致力于过完备(overcomplete)信号表示的研究.这种表示不同于许多传统的表示.因为它能提 ...
- C++ 顺序表
C++ 顺序表 /***1顺序表1.必做题 编写程序建立一个数续表,并逐个输出顺序表中所有数据元素的值.编写主函数测试结果. 编写顺序表定位操作子函数,在顺序表中查找是否存在数据元素x. 如果存在,返 ...
- HW6.15
import java.util.Scanner; import java.util.ArrayList; public class Solution { public static void mai ...
- SQL Server Cpu 100% 的常见原因及优化
SQL Server Cpu 100% 的情况并不太常见,一般引起 SQL Server 产生性能问题的,都是 阻塞.连接数.IO 磁盘等.所以,一般SQL Server 的使用率都是比较低的.但是, ...
- CSS样式表优先级
使用CSS样式表一共有2种方式:内部和外部,其中内部分为行内样式和嵌入式,外部分为导入式和链接式. 如果需要在不同的方式中设定同一个属性的时候,样式的优先级别就出现了. 测试代码如下: red.css ...