仿京東淘寶商品詳情頁上拉下拉效果

需求背景

電商類App普遍都會(huì)有商品詳情頁面萧锉,大家也普遍都會(huì)找京東淘寶進(jìn)行模仿,大家會(huì)發(fā)現(xiàn)谋作,京東淘寶的第一頁會(huì)有一個(gè)上拉會(huì)展示webview息裸,webview下拉又會(huì)返回上部分的效果。

預(yù)期目的

達(dá)到與京東淘寶類似的上拉下拉效果

image

實(shí)現(xiàn)原理

視圖的層級(jí)結(jié)構(gòu)如下圖

image

核心原理其實(shí)就是當(dāng)tableView上拉或者webView下拉后對(duì)transformView

進(jìn)行偏移岭洲,將其顯示在mainScrollView的contentSize中

具體實(shí)現(xiàn)

//
//  JDProductDeViewController.m
//  JDProductDetail
//
//  Created by 兩根手指敲代碼 on 2018/5/17.
//  Copyright ? 2018年 兩根手指敲代碼. All rights reserved.
//

#import "JDProductDeViewController.h"
#import "MJRefresh.h"

#define WS(weakSelf)  __weak __block __typeof(&*self)weakSelf = self;


#define kScreenWidth [[UIScreen mainScreen] bounds].size.width
#define kScreenHeight [[UIScreen mainScreen] bounds].size.height
#define IS_iPhoneX (kScreenWidth == 375.f && kScreenHeight == 812.f ? YES : NO)
#define TABBAR_SAFE_BOTTOM_HEIGHT (IS_iPhoneX ? 34.f : 0.f)
#define TABBAR_HEIGHT (TABBAR_SAFE_BOTTOM_HEIGHT + 49.f)
#define STATUSBAR_HEIGHT ([[UIApplication sharedApplication] statusBarFrame].size.height)
#define NAVBAR_HEIGHT (STATUSBAR_HEIGHT+44.f)

#define kEndHeight 80

@interface JDProductDeViewController ()<UIScrollViewDelegate, UITableViewDelegate,UITableViewDataSource>
@property (assign, nonatomic) CGFloat minY;
@property (assign, nonatomic) CGFloat maxY;
@property (assign, nonatomic) BOOL isShowBottom;
@property(nonatomic,strong) UIScrollView     *mainScrollView;//最底層橫向scrollview

@property(nonatomic,strong) UIView          *transformView;//用來做第一面偏移的view宛逗,是 detailTableView、webView的父視圖
@property(nonatomic,strong) UITableView     *detailTableView;//第一面tableview
@property(nonatomic,strong) UIWebView       *webView;//第一面webview

@property(nonatomic,strong) UIWebView       *scrollWebView;//第二面webview

@property(nonatomic,strong) UILabel         *bottomView;//底部菜單

@property(nonatomic,strong)UIPageControl *pageControl;
@end

@implementation JDProductDeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //最底層橫向scrollview
    _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, NAVBAR_HEIGHT, kScreenWidth, kScreenHeight-NAVBAR_HEIGHT-TABBAR_HEIGHT)];
    _mainScrollView.pagingEnabled = YES;
    _mainScrollView.backgroundColor = [UIColor whiteColor];
    _mainScrollView.delegate = self;
    _mainScrollView.contentSize = CGSizeMake(kScreenWidth*3, kScreenHeight-NAVBAR_HEIGHT-TABBAR_HEIGHT);
    [self.view addSubview:_mainScrollView];
    
    //用來做偏移的view
    _transformView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, (kScreenHeight-NAVBAR_HEIGHT-TABBAR_HEIGHT) * 2)];
    _transformView.backgroundColor = [UIColor grayColor];
    [_mainScrollView addSubview:_transformView];
    
    //第一面tableview
    _detailTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-NAVBAR_HEIGHT-TABBAR_HEIGHT)];
    _detailTableView.delegate = self;
    _detailTableView.dataSource = self;
    _detailTableView.backgroundColor = [UIColor magentaColor];
    [_transformView addSubview:self.detailTableView];
    WS(weakSelf)
    _detailTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakSelf transformToWebview];
    }];
    
    //第一面webview
    _webView = [[UIWebView alloc] init];
    _webView.backgroundColor = [UIColor magentaColor];
    _webView.frame = CGRectMake(0, kScreenHeight-NAVBAR_HEIGHT-TABBAR_HEIGHT, kScreenWidth, kScreenHeight-NAVBAR_HEIGHT-TABBAR_HEIGHT);
    _webView.scrollView.delegate = self;
    _webView.scrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf transformToTableview];
    }];
    [self.transformView addSubview:_webView];
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.didachuxing.com/static/h5/didahome/index.html"]]];
    
    
    //底部菜單
    _bottomView = [[UILabel alloc] initWithFrame:CGRectMake(0, kScreenHeight - TABBAR_HEIGHT, kScreenWidth, TABBAR_HEIGHT)];
    _bottomView.text = @"底部菜單欄(購(gòu)物車盾剩、購(gòu)買)";
    _bottomView.textAlignment = NSTextAlignmentCenter;
    _bottomView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:_bottomView];
    
    //第二面webview
    _scrollWebView = [[UIWebView alloc] init];
    _scrollWebView.backgroundColor = [UIColor orangeColor];
    _scrollWebView.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight-NAVBAR_HEIGHT-TABBAR_HEIGHT);
    _scrollWebView.scrollView.delegate = self;
    [_mainScrollView addSubview:_scrollWebView];
    
    [_scrollWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.didachuxing.com/static/h5/didahome/index.html"]]];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
    
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 200, 120, 30)];
    _pageControl.pageIndicatorTintColor = UIColor.greenColor;
    _pageControl.numberOfPages = 3;
    _pageControl.currentPageIndicatorTintColor = UIColor.orangeColor;
    [view addSubview:_pageControl];
    self.navigationItem.titleView = _pageControl;
}

#pragma _transformView偏移
- (void)transformToWebview {
    [self.detailTableView.mj_footer endRefreshing];
    //_transformView向下偏移
    [UIView animateWithDuration:0.5 animations:^{
        self.transformView.transform = CGAffineTransformTranslate(self.transformView.transform, 0,-(kScreenHeight-NAVBAR_HEIGHT-TABBAR_HEIGHT));
    } completion:^(BOOL finished) {
        self.mainScrollView.scrollEnabled = NO;
    }];
}

- (void)transformToTableview {
    [self.webView.scrollView.mj_header endRefreshing];
    
    //_transformView向上偏移
    [UIView animateWithDuration:0.5 animations:^{
        self.transformView.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        self.mainScrollView.scrollEnabled = YES;
    }];
}

#pragma tableview代理
- (NSInteger)numberOfSections {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 25;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    cell.backgroundColor = [UIColor greenColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    int page = scrollView.contentOffset.x / kScreenWidth + 0.5;
    _pageControl.currentPage = page;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

github地址: https://github.com/554994782/JDProductDetail.git

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末雷激,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子告私,更是在濱河造成了極大的恐慌屎暇,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,640評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件驻粟,死亡現(xiàn)場(chǎng)離奇詭異根悼,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)蜀撑,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,254評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門挤巡,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人酷麦,你說我怎么就攤上這事矿卑。” “怎么了沃饶?”我有些...
    開封第一講書人閱讀 165,011評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵母廷,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我绍坝,道長(zhǎng)徘意,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,755評(píng)論 1 294
  • 正文 為了忘掉前任轩褐,我火速辦了婚禮椎咧,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己勤讽,他們只是感情好蟋座,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,774評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著脚牍,像睡著了一般向臀。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上诸狭,一...
    開封第一講書人閱讀 51,610評(píng)論 1 305
  • 那天券膀,我揣著相機(jī)與錄音,去河邊找鬼驯遇。 笑死芹彬,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的叉庐。 我是一名探鬼主播舒帮,決...
    沈念sama閱讀 40,352評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼陡叠!你這毒婦竟也來了玩郊?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,257評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤枉阵,失蹤者是張志新(化名)和其女友劉穎译红,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體岭妖,經(jīng)...
    沈念sama閱讀 45,717評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡临庇,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,894評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了昵慌。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片假夺。...
    茶點(diǎn)故事閱讀 40,021評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖斋攀,靈堂內(nèi)的尸體忽然破棺而出已卷,到底是詐尸還是另有隱情,我是刑警寧澤淳蔼,帶...
    沈念sama閱讀 35,735評(píng)論 5 346
  • 正文 年R本政府宣布侧蘸,位于F島的核電站,受9級(jí)特大地震影響鹉梨,放射性物質(zhì)發(fā)生泄漏讳癌。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,354評(píng)論 3 330
  • 文/蒙蒙 一存皂、第九天 我趴在偏房一處隱蔽的房頂上張望晌坤。 院中可真熱鬧逢艘,春花似錦、人聲如沸骤菠。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,936評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽商乎。三九已至央拖,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間鹉戚,已是汗流浹背鲜戒。 一陣腳步聲響...
    開封第一講書人閱讀 33,054評(píng)論 1 270
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留崩瓤,地道東北人袍啡。 一個(gè)月前我還...
    沈念sama閱讀 48,224評(píng)論 3 371
  • 正文 我出身青樓踩官,卻偏偏與公主長(zhǎng)得像却桶,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子蔗牡,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,974評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 簡(jiǎn)介 最近幾天都在忙事情,有幾天沒有筆記了.今天總算是忙里偷閑,打算寫一下css3中濾鏡的簡(jiǎn)單用法. 內(nèi)容 fil...
    小貔閱讀 383評(píng)論 0 0
  • 人工智能颖系、機(jī)器學(xué)習(xí)、神經(jīng)網(wǎng)絡(luò)辩越、深度學(xué)習(xí)嘁扼、自然語言處理的關(guān)系 人工智能(AI):是目標(biāo),比如讓計(jì)算機(jī)下圍棋黔攒,讓機(jī)器人...
    被囚禁的無知閱讀 282評(píng)論 0 0
  • 2016年9月督惰,通億付股份正式推出旗下支付業(yè)務(wù)品牌“優(yōu)鳥錢包”不傅。2017年1月,在中華人民共和國(guó)版權(quán)局計(jì)算機(jī)軟件...
    王雪兒11閱讀 496評(píng)論 0 0
  • 記得一位名人曾經(jīng)說過崖疤,一個(gè)愛別人的人,先要學(xué)會(huì)愛自己典勇。實(shí)際上劫哼,我們的戀愛都是從愛對(duì)方開始的,從相識(shí)相知到密不可分割笙。...
    身邊的江湖閱讀 421評(píng)論 0 0
  • 《格局逆襲》的作者大熊說:“我天天自稱大熊老師权烧,這就是一種暗示,叫的久了,我就真覺得自己是個(gè)老師了豪嚎∩ν眨” ...
    不是什么偉大的人閱讀 221評(píng)論 0 1