简单实现浮动按钮+上滑隐藏按钮+下滑显示按钮

#import "SlideShowOrHideVC.h"

@interface SlideShowOrHideVC ()
{
    CGFloat lastContentOffset;
    BOOL    hideBtn;
    BOOL    isBottom;
}

@property (nonatomic, strong) UITableView   *tableView;
@property (nonatomic, strong) UIButton      *myButton;

@end

@implementation SlideShowOrHideVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-kStatusbarH-kNavigationbarH-kBottomSafeH);
    [self.view addSubview:self.tableView];
    
    self.myButton.frame = CGRectMake(kScreenWidth-60, kScreenHeight-kStatusbarH-kNavigationbarH-kBottomSafeH-100, 40, 40);
    [self.view addSubview:self.myButton];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat hight = scrollView.frame.size.height;
    CGFloat contentOffset = scrollView.contentOffset.y;
    CGFloat distanceFromBottom = scrollView.contentSize.height - contentOffset;
    CGFloat offset = contentOffset - lastContentOffset;
    lastContentOffset = contentOffset;

    __weak typeof (self) weakSelf = self;
    
    if (contentOffset == 0) {
        NSLog(@"滑动到顶部");
    }
    
    if (distanceFromBottom <= hight) {
        NSLog(@"滑动到底部");
        isBottom = YES;
    }
    
    if (offset > 0 && contentOffset > 0) {
        NSLog(@"上拉行为");
        if (hideBtn == NO && isBottom == NO) {  // 隐藏
            [UIView animateWithDuration:0.2 animations:^{
                weakSelf.myButton.frame = CGRectMake(kScreenWidth-60, kScreenHeight, 40, 40);
            }];
            hideBtn = YES;
        }
    }
    
    if (offset < 0 && distanceFromBottom > hight) {
        NSLog(@"下拉行为");
        if (hideBtn == YES && isBottom == NO) {  // 显示
            [UIView animateWithDuration:0.2 animations:^{
                weakSelf.myButton.frame = CGRectMake(kScreenWidth-60, kScreenHeight-kStatusbarH-kNavigationbarH-kBottomSafeH-100, 40, 40);
            }];
            hideBtn = NO;
        }
        isBottom = NO;
    }
}


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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"cellID";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0f;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0001f;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

#pragma mark - Lazy

- (UITableView *)tableView {
    if (_tableView != nil) {
        return _tableView;
    }
    
    _tableView                 = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.dataSource      = self;
    _tableView.delegate        = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.rowHeight       = 44;
    _tableView.separatorColor  = [UIColor lightGrayColor];
    _tableView.separatorInset  = UIEdgeInsetsZero;
    
    return _tableView;
}

- (UIButton *)myButton {
    if (_myButton != nil) {
        return _myButton;
    }
    
    _myButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
    _myButton.backgroundColor = [UIColor orangeColor];
    [_myButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    return _myButton;
}

@end

你可能感兴趣的:(简单实现浮动按钮+上滑隐藏按钮+下滑显示按钮)