您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 實現淘寶上拉進入詳情頁的交互效果

編輯:iOS 開發綜合

前言

本文主要介紹如何利用 iOS 實現淘寶中「上拉進入詳情頁」的交互效果,下面直接來看詳細的實作過程。

實作分析

可以看到,該頁面分為兩部分:一部分是一開始就能看到的商品資訊,另一部分則需上拉螢幕、隨滾動逐步出現。當滾動到第一部分結束時,底部會出現「繼續拖動,查看圖文詳情」一行提示文字;持續上拉到某個臨界點便觸發翻頁,此時第二部分以動畫形式從底部湧出並占滿整個螢幕,而且整體頁面是上移的,也就是第一部分與第二部分都會向上移動。

此時第二部分占滿整個螢幕,若我們下拉螢幕,則頂部會淡出「下拉,返回寶貝詳情」的文字提示;達到某個臨界值後文字變為「釋放,返回寶貝詳情」,此時鬆開手指,頁面又會滾動回第一部分的尾部。

實作方法

在我寫的 demo 中,第一部分是個 tableView,展示商品基本資訊;第二部分是個 webView,展示商品圖文詳情。

第一步先加載需要的視圖,主要是第一部分的 tableView 與第二部分的 webView,還有第二部分頂部用來顯示上拉返回提示的 headLab。為了節省資源,其實可以在上拉觸發時再加載第二部分視圖,但這裡僅作示範,因此並未做懶加載。

 - (void)loadContentView { // first view [self.contentView addSubview:self.tableView]; // second view [self.contentView addSubview:self.webView]; UILabel *hv = self.headLab; // headLab [self.webView addSubview:hv]; [self.headLab bringSubviewToFront:self.contentView]; } - (UILabel *)headLab { if(!_headLab){ _headLab = [[UILabel alloc] init]; _headLab.text = @"上拉,返回詳情"; _headLab.textAlignment = NSTextAlignmentCenter; _headLab.font = FONT(13); } _headLab.frame = CGRectMake(0, 0, PDWidth_mainScreen, 40.f); _headLab.alpha = 0.f; _headLab.textColor = PDColor_button_Gray; return _headLab; } - (UITableView *)tableView { if(!_tableView){ _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, PDWidth_mainScreen, self.contentView.bounds.size.height) style:UITableViewStylePlain]; // _tableView.contentSize = CGSizeMake(PDWidth_mainScreen, 800); _tableView.dataSource = self; _tableView.delegate = self; _tableView.rowHeight = 40.f; UILabel *tabFootLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, PDWidth_mainScreen, 60)]; tabFootLab.text = @"繼續拖動,查看圖文詳情"; tabFootLab.font = FONT(13); tabFootLab.textAlignment = NSTextAlignmentCenter; // tabFootLab.backgroundColor = PDColor_Orange; _tableView.tableFooterView = tabFootLab; } return _tableView; } - (UIWebView *)webView { if(!_webView){ _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, _tableView.contentSize.height, PDWidth_mainScreen, PDHeight_mainScreen)]; _webView.delegate = self; _webView.scrollView.delegate = self; [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]]; } return _webView; }

接著實作 UIScrollView 的委派方法,在裡面完成滾動到達臨界值後觸發翻頁動畫的處理,包含上拉翻到第二頁與下拉翻回第一頁兩部分,也就是要在該方法中透過判斷 scrollView 的類型做相應處理。

 #pragma mark ---- scrollView delegate -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { CGFloat offsetY = scrollView.contentOffset.y; if([scrollView isKindOfClass:[UITableView class]]) // tableView界面上的滾動 { // 能觸發翻頁的理想值:tableView整體的高度減去屏幕本省的高度 CGFloat valueNum = _tableView.contentSize.height -PDHeight_mainScreen; if ((offsetY - valueNum) > _maxContentOffSet_Y) { [self goToDetailAnimation]; // 進入圖文詳情的動畫 } } else // webView頁面上的滾動 { NSLog(@"-----webView-------"); if(offsetY<0 && -offsetY>_maxContentOffSet_Y) { [self backToFirstPageAnimation]; // 返回基本詳情界面的動畫 } } }

再看看兩個翻頁動畫,其實很簡單,就是移動它們的位置。

 // 進入詳情的動畫 - (void)goToDetailAnimation { [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionLayoutSubviews animations:^{ _webView.frame = CGRectMake(0, 0, PDWidth_mainScreen, PDHeight_mainScreen); _tableView.frame = CGRectMake(0, -self.contentView.bounds.size.height, PDWidth_mainScreen, self.contentView.bounds.size.height); } completion:^(BOOL finished) { }]; } // 返回第一個界面的動畫 - (void)backToFirstPageAnimation { [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionLayoutSubviews animations:^{ _tableView.frame = CGRectMake(0, 0, PDWidth_mainScreen, self.contentView.bounds.size.height); _webView.frame = CGRectMake(0, _tableView.contentSize.height, PDWidth_mainScreen, PDHeight_mainScreen); } completion:^(BOOL finished) { }]; }

接著還有第二頁下拉時螢幕頂部的文字提示動畫。這裡我們透過 KVO 監聽 webView 的 scrollView 偏移量,只要偏移量發生變化便會即時執行 KVO 的委派方法,然後在方法內依偏移量的變動完成動畫即可(隨著偏移量變大,文字逐漸變得透明,達到某個臨界點後文字變為紅色,內容也改為「釋放,返回詳情」)。

開始監聽 webView 滾動的偏移量:

 // 開始監聽_webView.scrollView的偏移量 [_webView.scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];

在 KVO 的委派方法中,依偏移量完成提示文字的動畫:

 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context { if(object == _webView.scrollView && [keyPath isEqualToString:@"contentOffset"]) { NSLog(@"----old:%@----new:%@",change[@"old"],change[@"new"]); [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(PDWidth_mainScreen/2, -offsetY/2.f); // 圖標翻轉,表示已超過臨界值,松手就會返回上頁 if(-offsetY>_maxContentOffSet_Y){ _headLab.textColor = [UIColor redColor]; _headLab.text = @"釋放,返回詳情"; }else{ _headLab.textColor = PDColor_button_Gray; _headLab.text = @"上拉,返回詳情"; } }

最終實現效果:

總結

以上就是本文的完整內容,希望對各位 iOS 開發者有所幫助,若有疑問歡迎留言交流。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved