iOS 仿淘宝,上拉进入详情页面
2016-12-04 10:19
239 查看
今天做的主要是一个模仿淘宝,上拉进入商品详情的功能,主要是通过 tableView 与 webView 一起来实现的,当然也可根据自己的需要把 webView 替换成你想要的
[objc] viewplain copy
//
// 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 = 60.f;
[self.view addSubview:_tableView];
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
UILabel *footLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60)];
footLabel.text = @"继续拖动,查看图文详情";
footLabel.font = [UIFont systemFontOfSize:13];
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:13];
_headLab.frame = CGRectMake(0, 0, self.view.frame.size.width, 40.f);
_headLab.alpha = 0.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(0, _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 15;
}
- (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) > 40)
{
[self goToDetailAnimation]; // 进入图文详情的动画
}
}
else // webView页面上的滚动
{
if(offsetY < 0 && -offsetY > 40)
{
[self backToFirstPageAnimation]; // 返回基本详情界面的动画
}
}
}
// 进入详情的动画
- (void)goToDetailAnimation
{
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionLayoutSubviews animations:^{
_webView.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height);
_tableView.frame = CGRectMake(0, -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(0, 0, self.view.frame.size.width, self.view.bounds.size.height);
_webView.frame = CGRectMake(0, _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:(voidvoid *)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/60;
_headLab.center = CGPointMake(self.view.frame.size.width/2, -offsetY/2.f);
// 图标翻转,表示已超过临界值,松手就会返回上页
if(-offsetY > 40){
_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 仿淘宝,上拉进入详情页面
- iOS仿淘宝,上拉进入详情页面
- iOS项目开发--实现类似淘宝详情页面
- ios仿淘宝商品详情页面粘贴商品规格弹出模板
- iOS实现淘宝上拉进入详情页交互效果
- ios 类似淘宝商品详情页面的效果
- 以淘宝详情页面Json为例使用Gson的JsonReader解析复杂Json数据详解
- Android自定义控件-仿淘宝ios客户端天猫商品详情界面动效
- android 仿淘宝详情页面,悬浮tab 选项卡实现方式
- iOS开发--跳转进入AppStore评论页面
- iOS——退出App程序;进入设置页面
- 仿淘宝商品详情页面下拉黏滞效果
- ios检测网络,无网络提示并直接进入设置页面
- iOS - 进入app显示相应的页面(不要每次都是登陆页啊 很蠢!)
- 代码: 仿淘宝商品详情页左上,图片鼠标浮上去,图片部分区域放大 (页面布局:图片列表)
- Android自定义控件-仿淘宝ios客户端天猫商品详情界面动效
- iOS 类似淘宝商品详情查看翻页效果的实现
- ios应用增加给appstore评分和进入app安装页面代码功能
- android webview中加入无线淘宝客页面点击宝贝详情链接进入宝贝详情页面后无法返回问题
- iOS 指纹识别进入页面时状态栏显示重影