iOS高仿簡書文章詳情頁面

類繼承于UIViewController,底部添加按鈕必須繼承于UIViewController,如果繼承于UITableViewController底部的按鈕會跟著一起滾動,不會懸浮在底部。直接上代碼,其實沒幾行代碼,這里我把完整的代碼貼出來

@interface MyTableViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic ,strong)UITableView * tableView;
@property (nonatomic, strong)UIView * bottomButtonView;
@property (nonatomic,assign)CGFloat offsetY;
@end```
```codelang
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"高仿簡書文章詳情頁面";
    self.offsetY = 0.0;
    [self initTableView];
    [self creatButton];
    [self initStateBarView];
}```
```codelang
-(void)initStateBarView{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, 20)];
    view.backgroundColor = [UIColor whiteColor];//狀態(tài)欄顏色
    [self.view addSubview:view];
}```

```codelang
-(void)initTableView{
//初始化UITableView
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
}```
```codelang
-(void)creatButton{
//創(chuàng)建底部按鈕
    self.bottomButtonView = [[UIView alloc]init];
    self.bottomButtonView.frame = CGRectMake(0, self.view.frame.size.height - 50, self.view.frame.size.width, 50);
    self.bottomButtonView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.bottomButtonView];
    UIButton  *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame =  CGRectMake(0, 0, self.view.frame.size.width / 2, 50);
    [button setTitle:@"插播廣告" forState:UIControlStateNormal];
    button.backgroundColor =[UIColor orangeColor];
    button.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    button.layer.borderWidth = 1;
    [self.bottomButtonView addSubview:button];
    
    UIButton  * butn= [UIButton buttonWithType:UIButtonTypeCustom];
    butn.frame =  CGRectMake(button.frame.size.width , 0, self.view.frame.size.width / 2, 50);
    [butn setTitle:@"推廣聯(lián)盟" forState:UIControlStateNormal];
    butn.backgroundColor =[UIColor redColor];
    butn.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    butn.layer.borderWidth = 1;
    [self.bottomButtonView addSubview:butn];    
}```
```codelang
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * cellIndetifi = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifi];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIndetifi];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
    // Configure the cell...   
    return cell;
}```
```codelang
#pragma mark ====== UIScrollViewDelegete ===========
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    self.offsetY = scrollView.contentOffset.y;
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y >self.offsetY ) {//向上滑動
        //按鈕消失
        [UIView transitionWithView:self.bottomButtonView duration:0.1 options:UIViewAnimationOptionTransitionNone animations:^{
            self.bottomButtonView.frame = CGRectMake(0, self.view.frame.size.height , self.view.frame.size.width, 50);
            [self.navigationController setNavigationBarHidden:NO animated:YES];
        } completion:NULL];
        
    }else if (scrollView.contentOffset.y < self.offsetY ){//向下滑動
        //按鈕出現(xiàn)
        [UIView transitionWithView:self.bottomButtonView duration:0.1 options:UIViewAnimationOptionTransitionNone animations:^{
            self.bottomButtonView.frame = CGRectMake(0, self.view.frame.size.height - 50, self.view.frame.size.width, 50);
            [self.navigationController setNavigationBarHidden:YES animated:YES];
        } completion:NULL];
    }
    NSLog(@"偏移量是多少==%f",self.offsetY);
}```
```codelang
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGPoint translation = [scrollView.panGestureRecognizer translationInView:scrollView.superview];
    if (translation.y>0) {
        //按鈕消失
        [UIView transitionWithView:self.bottomButtonView duration:0.2 options:UIViewAnimationOptionTransitionNone animations:^{
            self.bottomButtonView.frame = CGRectMake(0, self.view.frame.size.height - 50, self.view.frame.size.width, 50);
        [self.navigationController setNavigationBarHidden:NO animated:YES];
        } completion:NULL];
    }else if(translation.y<0){
        //按鈕出現(xiàn)
        [UIView transitionWithView:self.bottomButtonView duration:0.2 options:UIViewAnimationOptionTransitionNone animations:^{
            self.bottomButtonView.frame = CGRectMake(0, self.view.frame.size.height , self.view.frame.size.width, 50);
            [self.navigationController setNavigationBarHidden:YES animated:YES];
        } completion:NULL];
    }
}```
代碼就這么多,下面上效果圖
![仿簡書文章詳情頁面.gif](http://upload-images.jianshu.io/upload_images/969236-5de860d7bd426f8f.gif?imageMogr2/auto-orient/strip)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容