转::iOS 仿淘宝,上拉进入详情页面
今天做的主要是一个模仿淘宝,上拉进入商品详情的功能,主要是通过 tableView 与 webView 一起来实现的,当然也可根据自己的需要把 webView 替换成你想要的
//
// ViewController.m
// 仿淘宝,上拉进入详情
//
// Created by Amydom on 16/11/22.
// Copyright © 2016年 Amydom. All rights reserved.
// #import "ViewController.h" @interface ViewController ()<UITableViewDelegate , UITableViewDataSource , UIScrollViewDelegate , UIWebViewDelegate> @property (nonatomic , strong)UITableView *tableView; @property (nonatomic , strong)UIWebView *webView; @property (nonnull , strong)UILabel *headLab; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor]; [self createView]; } - (void)createView{ _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.rowHeight = .f;
[self.view addSubview:_tableView];
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"]; UILabel *footLabel = [[UILabel alloc]initWithFrame:CGRectMake(, , self.view.frame.size.width, )];
footLabel.text = @"继续拖动,查看图文详情";
footLabel.font = [UIFont systemFontOfSize:];
footLabel.textAlignment = NSTextAlignmentCenter;
_tableView.tableFooterView = footLabel;
//注意:懒加载时,只有用 self 才能调其 getter 方法
[self.view addSubview:self.webView];
_headLab = [[UILabel alloc] init];
_headLab.text = @"上拉,返回详情";
_headLab.textAlignment = NSTextAlignmentCenter;
_headLab.font = [UIFont systemFontOfSize:];
_headLab.frame = CGRectMake(, , self.view.frame.size.width, .f);
_headLab.alpha = .f;
_headLab.textColor = [UIColor blackColor];
[_webView addSubview:_headLab]; [ _webView.scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil]; } //懒加载 webView 增加流畅度
- (UIWebView *)webView{ //注意,这里不用 self 防止循环引用
if (!_webView) {
_webView = [[UIWebView alloc]initWithFrame:CGRectMake(, _tableView.contentSize.height, self.view.frame.size.width, self.view.frame.size.height)];
_webView.delegate = self;
_webView.delegate = self;
_webView.scrollView.delegate = self; [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]];
} return _webView; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return ; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *indetifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indetifier];
cell.textLabel.text = @"Amydom"; return cell; } //监测 scroll 的偏移量
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
CGFloat offsetY = scrollView.contentOffset.y; if([scrollView isKindOfClass:[UITableView class]]) // tableView界面上的滚动
{
// 能触发翻页的理想值:tableView整体的高度减去屏幕本省的高度
CGFloat valueNum = _tableView.contentSize.height - self.view.frame.size.height;
if ((offsetY - valueNum) > )
{ [self goToDetailAnimation]; // 进入图文详情的动画
}
} else // webView页面上的滚动
{
if(offsetY < && -offsetY > )
{
[self backToFirstPageAnimation]; // 返回基本详情界面的动画
}
}
} // 进入详情的动画
- (void)goToDetailAnimation
{
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionLayoutSubviews animations:^{
_webView.frame = CGRectMake(, , self.view.frame.size.width, self.view.frame.size.height);
_tableView.frame = CGRectMake(, -self.view.frame.size.height , self.view.frame.size.width, self.view.frame.size.height);
} completion:^(BOOL finished) { }];
} // 返回第一个界面的动画
- (void)backToFirstPageAnimation
{
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionLayoutSubviews animations:^{
_tableView.frame = CGRectMake(, , self.view.frame.size.width, self.view.bounds.size.height);
_webView.frame = CGRectMake(, _tableView.contentSize.height, self.view.frame.size.width, self.view.frame.size.height); } completion:^(BOOL finished) { }];
} // KVO观察
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{ if(object == _webView.scrollView && [keyPath isEqualToString:@"contentOffset"])
{
[self headLabAnimation:[change[@"new"] CGPointValue].y];
}else
{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
} // 头部提示文本动画
- (void)headLabAnimation:(CGFloat)offsetY
{
_headLab.alpha = -offsetY/;
_headLab.center = CGPointMake(self.view.frame.size.width/, -offsetY/.f);
// 图标翻转,表示已超过临界值,松手就会返回上页
if(-offsetY > ){
_headLab.textColor = [UIColor redColor];
_headLab.text = @"释放,返回详情";
}else{
_headLab.textColor = [UIColor blackColor];
_headLab.text = @"上拉,返回详情";
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
转::iOS 仿淘宝,上拉进入详情页面的更多相关文章
- 仿淘宝使用flex布局实现页面顶部和底部的固定布局
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- Android仿淘宝继续上拉进入商品详情页的效果,使用双Fragment动画切换;
仿淘宝继续上拉进入商品详情页的效果,双Fragment实现: 动画效果: slide_above_in.xml <?xml version="1.0" encoding=&q ...
- 仿淘宝头像上传功能(三)——兼容 IE6 浏览器。
前两篇目录: 仿淘宝头像上传功能(一)——前端篇. 仿淘宝头像上传功能(二)——程序篇. 仿淘宝头像上传功能(三)——兼容 IE6 浏览器 之前的这两篇虽然实现了功能,但不兼容低版本浏览器,而且有些浏 ...
- 基于Bootstrap仿淘宝分页控件实现
.header { cursor: pointer } p { margin: 3px 6px } th { background: lightblue; width: 20% } table { t ...
- 高仿淘宝和聚美优品商城详情页实现《IT蓝豹》
高仿淘宝和聚美优品商城详情页实现 android-vertical-slide-view高仿淘宝和聚美优品商城详情页实现,在商品详情页,向上拖动时,可以加载下一页. 使用ViewDragHelper, ...
- android版高仿淘宝客户端源码V2.3
android版高仿淘宝客户端源码V2.3,这个版本我已经更新到2.3了,源码也上传到源码天堂那里了,大家可以看一下吧,该应用实现了我们常用的购物功能了,也就是在手机上进行网购的流程的,如查看产品(浏 ...
- 仿淘宝左侧菜单导航栏纯Html + css 写的
这俩天闲来没事淘宝逛了一圈看到淘宝的左侧导航菜单做的是真心的棒啊,一时兴起,查了点资料抓了几个图片仿淘宝写了个css,时间紧写的不太好,大神勿喷,给小白做个参考 废话不多说先来个效果图 接下来直接上代 ...
- Android仿淘宝头条滚动广告条
之前我使用TextView+Handler+动画,实现了一个简单的仿淘宝广告条的滚动,https://download.csdn.net/download/qq_35605213/9660825: 无 ...
- Android中仿淘宝首页顶部滚动自定义HorizontalScrollView定时水平自动切换图片
Android中仿淘宝首页顶部滚动自定义HorizontalScrollView定时水平自动切换图片 自定义ADPager 自定义水平滚动的ScrollView效仿ViewPager 当遇到要在Vie ...
随机推荐
- http 调用错误处理
1. http code 在使用Nginx时,经常会碰到502 Bad Gateway和504 Gateway Time-out错误,下面以Nginx+PHP-FPM来分析下这两种常见错误的原因和解决 ...
- <%%>用法初步认识
<%%>是用于向客户端插入服务器代码所使用的一种标记 例如为了在HTML页面上展示由服务器提供的当前用户的某条信息或名字等便可使用 前台 <a href="home.asp ...
- 初学Docker
1.基本概念Docker 包括三个基本概念镜像( Image )容器( Container )仓库( Repository )理解了这三个概念,就理解了 Docker 的整个生命周期. 2.Docke ...
- Linux-准备工作
首先安装一个box,安装一个centos7,然后就是xshell,接下来就是 查看ip ifconfig ip addr vi /etc/sysconfig/network-scripts/ifcfg ...
- Expect自动化交互程序
Expect介绍: 1.什么是Expect Expect是一个用来实现自动化交互功能的软件套件,基于TCL的脚本编程工具语言,方便学习,功能强大. 2.为什么要使用expcet: 当今的企业运维中,自 ...
- Java流(Stream)、文件(File)和IO
Java.io包几乎包含了所有操作输入.输出需要的类.所有这些流类代表了输入源和输出目标. Java.io包中的流支持很多格式,比如:基本类型.对象.本地化字符集等等. 一个流可以理解为一个数据的序列 ...
- Docker从零到实践过程中的坑
欢迎指正: Centos7 下的ulimit在Docker中的坑 http://www.dockone.io/article/522 僵尸容器:Docker 中的孤儿进程 https://yq.ali ...
- jvm探秘之三:GC初步
GC即垃圾收集器,虚拟机的必要组成部分. 不过这里说当然是,hotspot虚拟机(jvm的主要版本)的GC机制,前面说过了jvm的组成部分,那么想当然GC只需要负责方法区和堆就好了,虚拟机栈.本地方法 ...
- loj2058 「TJOI / HEOI2016」求和
推柿子 第二类斯特林数的容斥表达 fft卡精度就用ntt吧qwq. #include <iostream> #include <cstdio> using namespace ...
- Java中Scanner中nextLine()方法和next()方法的区别
https://blog.csdn.net/hello_word2/article/details/54895106