悬浮滚动隐藏按钮

2018-03-06  本文已影响82人  ___1o_8o
//发帖按钮
@property (nonatomic, strong) UIButton *postButton;
- (void)prepareHomePostButton {
    [self.view addSubview:self.postButton];
    [self.postButton setFrame:CGRectMake(self.view.width - 50 - 15, self.view.height - 50 - 30 - [BMUtils tabbarBarHeight], 50, 50)];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {   
    if (scrollView.dragging) {
        //手势触发的滚动则隐藏发帖按钮
        [self postButtonAnimationWithHidden:YES];
    }  
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self postButtonAnimationWithHidden:NO];
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    [self postButtonAnimationWithHidden:NO];
}
- (void)postButtonAnimationWithHidden:(BOOL)hidden {
    self.stopScrollTime = [[NSDate date] timeIntervalSince1970];
    BMWeakSelf
    if (!hidden) {
        CGFloat time = 0.7;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSTimeInterval nowTime = [[NSDate date] timeIntervalSince1970];
            if (nowTime - weakSelf.stopScrollTime >= time) {
                [UIView animateWithDuration:0.3 animations:^{
                    weakSelf.postButton.alpha = 1.0;
                }];
            }
        });
    }
    else {
        if (self.postButton.alpha < 0.01) {
            return;
        }
        [UIView animateWithDuration:0.3 animations:^{
            weakSelf.postButton.alpha = 0.0;
        }];
    }
}
上一篇下一篇

猜你喜欢

热点阅读