最近模仿京东顶部搜索条效果制作的一个小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. 数列极限---和Gauss(取整)函数有关

  2. HDU 3085 Nightmare Ⅱ 双向BFS

    题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...

  3. google学术反向代理及IPV6免流量上网【教育网BUPT】

    google反向代理 google https://awk.so/ 学术反向代理 https://awk.so/scholar/?hl=zh-CN 2015年1.1号开始流量计费,2元/G 无VPS用 ...

  4. <转>SpringMVC与Struts2 比较总结

    原链接:http://blog.csdn.net/chenleixing/article/details/44570681 个人整理: 1.级别不同:SpringMVC :方法  Struts是 类级 ...

  5. ACM2026

    /* 首字母变大写 Problem Description 输入一个英文句子,将每个单词的第一个字母改成大写字母.   Input 输入数据包含多个测试实例,每个测试实例是一个长度不超过100的英文句 ...

  6. sprintf的缓冲区溢出

    sprintf的缓冲区溢出 分类: 技术2010-03-07 15:26 362人阅读 评论(0) 收藏 举报 今天,调试sector的时候遇到一个特奇怪的问题,程序会在取string的c_str() ...

  7. leetcode@ [134] Gas station (Dynamic Programming)

    https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...

  8. android 的开源输入法介绍,及 自动触摸的实现方法

    输入法的开源代码见我自己的360云盘里的 openwnn-legacy-android-open-ime.tar.bz2 文件 http://www.pocketmagic.net/injecting ...

  9. 将Sublime Text 2搭建成一个好用的IDE

    将Sublime Text 2搭建成一个好用的IDE 说起编辑器,可能大部分人要推荐的是Vim和Emacs,本人用过Vim,功能确实强大,但是不是很习惯,之前一直有朋友推荐SUblime Text 2 ...

  10. 第十三章、学习 Shell Scripts

    什么是 Shell scripts shell script (程序化脚本) :shell script 是针对 shell 所写的『脚本!』 shell script 是利用 shell 的功能所写 ...