#import "SlideShowOrHideVC.h"
@interface SlideShowOrHideVC ()<UITableViewDataSource,UITableViewDelegate>
{
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(@"滑動(dòng)到頂部");
}
if (distanceFromBottom <= hight) {
NSLog(@"滑動(dòng)到底部");
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
簡(jiǎn)單實(shí)現(xiàn)浮動(dòng)按鈕+上滑隱藏按鈕+下滑顯示按鈕
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 效果圖 主要是在布局中展示,貼上布局 說(shuō)明
- 1》//滑動(dòng)隱藏導(dǎo)航欄,簡(jiǎn)書(shū)效果 -(void)scrollViewDidScroll:(UIScrollView...
- 在上篇博客 CoordinatorLayout、AppBarLayout實(shí)現(xiàn)上滑隱藏圖片,下滑顯示圖片的基礎(chǔ)上,仿...
- 效果圖 通過(guò)手勢(shì)改變導(dǎo)航欄的顯示和隱藏 實(shí)現(xiàn)思路 通過(guò)給頂部導(dǎo)航欄添加,移除 class 來(lái)實(shí)現(xiàn)隱藏和顯示的動(dòng)畫(huà) ...