iOS

上拉进入详情页

2017-01-09  本文已影响25人  何必轻言流年

依赖MJRefresh

实现

@import WebKit;
@interface GooddetailVC ()<UITableViewDelegate,UITableViewDataSource,WKNavigationDelegate>
@property (strong, nonatomic) UIScrollView *bgScrollView;
@property (strong, nonatomic) UITableView *subTableView;
@property (strong, nonatomic) WKWebView *webView;


@end

@implementation GooddetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self createView];
}

- (void)createView{
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.bgScrollView];
    [self.bgScrollView addSubview:self.subTableView];
    [self.bgScrollView addSubview:self.webView];
    
    [self setSubTableViewRefreshFooter];
    [self setWebViewRefreshHeader];
    
    for (int i=0; i<4; i++) {
        UIButton *btn =[[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/4 *i, SCREEN_HEIGHT-40, SCREEN_WIDTH/4, 40)];
        NSString *name =[NSString stringWithFormat:@"buy%d",i+1];
        [btn setBackgroundImage:[UIImage imageNamed:name]forState:UIControlStateNormal];
        [self.view addSubview:btn];
        if (i==3) {
            [btn addTarget:self action:@selector(buyaction) forControlEvents:UIControlEventTouchUpInside];
        }
    }
}

#pragma mark - Property
    - (UIScrollView *)bgScrollView
    {
        
        if (!_bgScrollView) {
            _bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kSMainScreenWidth, kSMainScreenHeight)];
            /*
             底层ScrollView必须scrollEnabled = NO
             */
            _bgScrollView.scrollEnabled = NO;
            _bgScrollView.backgroundColor = [UIColor lightTextColor];
            _bgScrollView.contentSize = CGSizeMake(kSMainScreenWidth, kSMainScreenHeight*2);
        }
        return _bgScrollView;
    }
    - (UITableView *)subTableView
    {
        if (!_subTableView) {
            _subTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kSMainScreenWidth, kSMainScreenHeight-104) style:UITableViewStylePlain];
            _subTableView.delegate = self;
            _subTableView.dataSource = self;
            [_subTableView registerNib:[UINib nibWithNibName:@"GoodtopCell" bundle:nil]forCellReuseIdentifier:@"Goodtop Cell"];
            [_subTableView registerNib:[UINib nibWithNibName:@"JieshaoCell" bundle:nil]forCellReuseIdentifier:@"Jieshao Cell"];
            [_subTableView registerNib:[UINib nibWithNibName:@"JieshaodetailCell" bundle:nil]forCellReuseIdentifier:@"Jieshaodetail Cell"];
        }
        return _subTableView;
    }
    - (WKWebView *)webView
    {
        if (!_webView) {
            _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, kSMainScreenHeight, kSMainScreenWidth, kSMainScreenHeight-104)];
            _webView.backgroundColor = [UIColor whiteColor];
            _webView.navigationDelegate=self;
            [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.bing.com"]]];
        }
        return _webView;
    }
#pragma mark - UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{  
 if(indexPath.section==0){
   GoodtopCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Goodtop Cell" forIndexPath:indexPath];
    return cell;
}else if(indexPath.section==1){
    JieshaoCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Jieshao Cell" forIndexPath:indexPath];
     return cell;
}else{
    JieshaodetailCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Jieshaodetail Cell" forIndexPath:indexPath];
    return cell;

}
  
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    OrganizationViewController * OGVC = [[OrganizationViewController alloc]init];
//    [self.navigationController pushViewController:OGVC animated:YES];
//    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
            return 180;
        case 1:
            return 480;
        default:
            return 320;
    }
    
    
}
#pragma mark - SetUp
    - (void)setSubTableViewRefreshFooter
    {
        __weak typeof(self) weakSelf = self;
        
        MJRefreshBackNormalFooter *refreshFooter = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            
            [weakSelf.bgScrollView scrollRectToVisible:CGRectMake(0, self.webView.originY-64, kSMainScreenWidth, kSMainScreenHeight) animated:YES];
            
            [weakSelf.subTableView.mj_footer endRefreshing];
//            [weakSelf.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.bing.com"]]];
        }];
        
        refreshFooter.arrowView.image = nil;
        
        [refreshFooter setTitle:@"上拉查看图文详情" forState:MJRefreshStateIdle];
        [refreshFooter setTitle:@"释放查看图文详情" forState:MJRefreshStatePulling];
        [refreshFooter setTitle:@"释放查看图文详情" forState:MJRefreshStateRefreshing];
        [refreshFooter setTitle:@"释放查看图文详情" forState:MJRefreshStateWillRefresh];
        [refreshFooter setTitle:@"上拉查看图文详情" forState:MJRefreshStateNoMoreData];
        // 隐藏状态
        //    refreshFooter.stateLabel.hidden = YES;
        self.subTableView.mj_footer = refreshFooter;
        
    }
    - (void)setWebViewRefreshHeader
    {
        __weak typeof(self) weakSelf = self;
        
        MJRefreshNormalHeader *refreshHeader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf.bgScrollView scrollRectToVisible:CGRectMake(0, 0, kSMainScreenWidth, kSMainScreenHeight) animated:YES];
            [weakSelf.webView.scrollView.mj_header endRefreshing];
        }];
        refreshHeader.lastUpdatedTimeLabel.hidden = YES;
        [refreshHeader setTitle:@"下拉回到商品详情" forState:MJRefreshStateIdle];
        [refreshHeader setTitle:@"释放回到商品详情" forState:MJRefreshStatePulling];
        [refreshHeader setTitle:@"释放回到商品详情" forState:MJRefreshStateRefreshing];
        [refreshHeader setTitle:@"释放回到商品详情" forState:MJRefreshStateWillRefresh];
        [refreshHeader setTitle:@"下拉回到商品详情" forState:MJRefreshStateNoMoreData];
        
        self.webView.scrollView.mj_header = refreshHeader;
    }

#pragma mark - WKNavigationDelegate
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    NSLog(@"didStartProvisionalNavigation");
    [FanProgress3D showInView:self.view status:@"正在加载"];
}
// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
    NSLog(@"didCommitNavigation");
    [FanProgress3D dismiss];
}
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    NSLog(@"didFinishNavigation");
    [FanProgress3D dismiss];
}
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation
{
    [FanProgress3D dismissWithStatus:@"请检查网络,返回重试" afterDelay:2];
}
-(void)viewWillAppear:(BOOL)animated{
    
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
//    [_webView stopLoading];    
}

示例图:


上拉加载详情 GIF.gif
上一篇下一篇

猜你喜欢

热点阅读