iOS高仿简书文章详情页面

96
我的天空蔚蓝色
2016.01.18 10:09* 字数 71

类继承于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];//状态栏颜色
    [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{
//创建底部按钮
    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:@"推广联盟" 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 ){//向下滑动
        //按钮出现
        [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){
        //按钮出现
        [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)
日记本
Web note ad 1