今天做的主要是一个模仿淘宝,上拉进入商品详情的功能,主要是通过tableView与webView一起来实现的,当然也可以根据自己的需要把webView替换成你想要的
- //
- // ViewController.m
- //仿淘宝,上拉进入详情
- //
- //由Amydom在16/11/22创建。
- //版权所有©2016年Amydom。版权所有。
- //
- #import“ViewController.h”
- @interface ViewController()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate,UIWebViewDelegate>
- @property (nonatomic , strong )UITableView * tableView;
- @property (nonatomic , strong )UIWebView * webView;
- @property (nonnull , strong )UILabel * headLab;
- @结束
- @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 = 6 0 .F ;
- [ self .view addSubview :_tableView];
- [_tableView registerClass :[UITableViewCell 类] forCellReuseIdentifier :@“cell” ];
- 的UILabel * footLabel = [[的UILabel 的alloc ] initWithFrame:方法CGRectMake(0 , 0 , 自.view .frame .size .WIDTH , 6 0)];
- footLabel .text = @“继续拖动,查看图文详情” ;
- footLabel .font = [UIFont systemFontOfSize :1 3 ];
- footLabel .textAlignment = NSTextAlignmentCenter;
- _tableView .tableFooterView = footLabel;
- //注意:懒加载时,只有用自才能调其getter方法
- [ self .view addSubview :self .webView ];
- _headLab = [[UILabel alloc ] init ];
- _headLab .text = @“上拉,返回详情” ;
- _headLab .textAlignment = NSTextAlignmentCenter;
- _headLab .font = [UIFont systemFontOfSize :1 3 ];
- _headLab .frame = CGRectMake(0 , 0 , 自.view .frame .size .WIDTH , 4 0 .F );
- _headLab 阿尔法 = 0 .F ;
- _headLab .textColor = [UIColor blackColor ];
- [_webView addSubview :_headLab];
- [_webView .scrollView addObserver :self forKeyPath :@“contentOffset” 选项:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld 上下文:nil ];
- }
- //懒加载webView增加流畅度
- - (UIWebView *)webView {
- //注意,这里不用自我防止循环引用
- 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” ]]];
- }
- 返回 _webView;
- }
- - (NSInteger)tableView :( UITableView *)tableView numberOfRowsInSection :(NSInteger)section {
- 返回1 5 ;
- }
- - (UITableViewCell *)tableView :( UITableView *)tableView cellForRowAtIndexPath :( NSIndexPath *)indexPath {
- 静态NSString * indetifier = @“cell” ;
- UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier :indetifier];
- 细胞.textLabel 的.text = @ “Amydom” ;
- 返回 单元格
- }
- //监测卷的偏移量
- - (void )scrollViewDidEndDragging :( UIScrollView *)scrollView willDecelerate :( BOOL )减速
- {
- 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)> 4 0 )
- {
- [ self goToDetailAnimation ]; //进入图文详情的动画
- }
- }
- else // webView页面上的滚动
- {
- if (offsetY < 0 && -offsetY> 4 0 )
- {
- [ self backToFirstPageAnimation ]; //返回基本详情界面的动画
- }
- }
- }
- //进入详情的动画
- - (void )goToDetailAnimation
- {
- [UIView animateWithDuration :0 .3 delay :0 .0 options :UIViewAnimationOptionLayoutSubviews animations :^ {
- _webView .frame = CGRectMake(0 , 6 4 , 自.view .frame .size .WIDTH , 自.view .frame .size .height );
- _tableView .frame = CGRectMake(0 , - self .view .frame .size .height , self .view .frame .size .width , self .view.frame .size .height );
- } 完成:^(BOOL 完成){
- }];
- }
- //返回第一个界面的动画
- - (void )backToFirstPageAnimation
- {
- [UIView animateWithDuration :0 .3 delay :0 .0 options :UIViewAnimationOptionLayoutSubviews animations :^ {
- _tableView .frame = CGRectMake(0 , 0 , 自.view .frame .size .WIDTH , 自.view .bounds .size .height );
- _webView .frame = CGRectMake(0 ,_tableView .contentSize .height , self .view .frame .size .width , self .view .frame.size .height );
- } 完成:^(BOOL 完成){
- }];
- }
- // KVO观察
- - (void )observeValueForKeyPath :( NSString *)keyPath ofObject :( id )object change :(NSDictionary <NSKeyValueChangeKey,id > *)change context (void void *)context {
- if (object == _webView .scrollView && [keyPath isEqualToString :@“contentOffset” ])
- {
- [ self headLabAnimation :[change [ @“new” ] CGPointValue ] .y ];
- } 其他
- {
- [ super observeValueForKeyPath :keyPath ofObject :object change :change context :context];
- }
- }
- //头部提示文本动画
- - (void )headLabAnimation:(CGFloat)offsetY
- {
- _headLab 阿尔法 = -offsetY / 6 0 ;
- _headLab .center = CGPointMake(self .view .frame .size .width / 2 ,-offsetY / 2 .f );
- //图标翻转,表示已超过临界值,松手就会返回上页
- if (-offsetY> 4 0 ){
- _headLab .textColor = [UIColor redColor ];
- _headLab .text = @“释放,返回详情” ;
- } else {
- _headLab .textColor = [UIColor blackColor ];
- _headLab .text = @“上拉,返回详情” ;
- }
- }
- - (void )didReceiveMemoryWarning {
- [ super didReceiveMemoryWarning ];
- //处理可以重新创建的任何资源。
- }
- @结束