Android开发中在TableView上添加悬浮按钮的方法

Android开发中在TableView上添加悬浮按钮的方法,第1张

概述如果直接在TableVIewController上贴Button的话会导致这个会随之滚动,下面解决在TableView上实现位置固定悬浮按钮的两种方法:

如果直接在tableVIEwController上贴button的话会导致这个会随之滚动,下面解决在tableVIEw上实现位置固定悬浮按钮的两种方法:

  1.在vIEw上贴tableVIEw,然后将悬浮按钮贴在vIEw的最顶层

  2.使用window

首先看一下最终的效果,在tableVIEwController上添加一个悬浮按钮,该按钮不能随着视图的滚动而滚动

首先介绍上面的第一种方法:

1)创建tablevIEw和底部按钮的属性

//屏幕宽#define kScreenW [UIScreen mainScreen].bounds.size.wIDth//屏幕高#define kScreenH [UIScreen mainScreen].bounds.size.height@interface broadcastliveVIEwController ()<UItableVIEwDataSource,UItableVIEwDelegate>@property(nonatomic) UItableVIEw *livesListtable;@property(nonatomic) UIbutton *bottombutton;@end

2)创建属性到最顶部

@implementation broadcastliveVIEwController- (voID)vIEwDIDLoad {[super vIEwDIDLoad];  CGRect clIEntRect = [UIScreen mainScreen].bounds;  _livesListtable = [[UItableVIEw alloc] initWithFrame:CGRectMake(0,clIEntRect.size.wIDth,clIEntRect.size.height-65) style:UItableVIEwStylePlain];[self.vIEw addSubvIEw:_livesListtable];_livesListtable.delegate = self;_livesListtable.dataSource = self; self.bottombutton = [UIbutton buttonWithType:UIbuttonTypeCustom];self.bottombutton.frame = CGRectMake(kScreenW - 80,kScreenH - 140,60,60);[self.bottombutton setBackgroundImage:[UIImage imagenamed:@"recordlive"] forState:UIControlStatenormal];[self.bottombutton addTarget:self action:@selector(onTapliveBtn) forControlEvents:UIControlEventtouchUpInsIDe];[self.vIEw addSubvIEw:self.bottombutton];

3)实现按钮事件

- (voID)onTapliveBtn{NSLog(@"点击底部按钮");}

接下来介绍第二种方法:

1)创建一个window,button属性避免window被释放

//屏幕宽#define kScreenW [UIScreen mainScreen].bounds.size.wIDth//屏幕高#define kScreenH [UIScreen mainScreen].bounds.size.height@interface broadcastliveVIEwController ()<UItableVIEwDataSource,UItableVIEwDelegate>@property(strong,nonatomic)UIWindow *window;@property(strong,nonatomic)UIbutton *button;@end

2)创建window和button

默认的情况下系统只有一个window这时我们需要设置windowLevel

window不用添加在任何视图上

- (voID)createbutton{_button = [UIbutton buttonWithType:UIbuttonTypeCustom];[_button setBackgroundImage:[UIImage imagenamed:@"recordlive"] forState:UIControlStatenormal];_button.frame = CGRectMake(0,60);[_button addTarget:self action:@selector(onTapliveBtn) forControlEvents:UIControlEventtouchUpInsIDe];_window = [[UIWindow alloc]initWithFrame: CGRectMake(kScreenW - 80,kScreenH - 80,60);];_window.windowLevel = UIWindowLevelAlert+1;_window.backgroundcolor = [UIcolor redcolor];_window.layer.cornerRadius = 30;_window.layer.masksToBounds = YES;[_window addSubvIEw:_button];[_window makeKeyAndVisible];//关键语句,显示window}

3)延时加载window,注意我们需要在rootwindow创建完成之后再创建这个悬浮的按钮

- (voID)vIEwDIDLoad {[super vIEwDIDLoad];[self performSelector:@selector(createbutton) withObject:nil afterDelay:1]; }

4)实现按钮事件

- (voID)onTapliveBtn{NSLog(@"点击底部按钮");}

注意::最后再添加一个小功能,使tablevIEw上下滑动的时候,按钮动画效果的出现和消失,在这里是上拉消失,下拽出

-(voID)scrollVIEwDIDScroll:(UIScrollVIEw *)scrollVIEw{if (scrollVIEw.contentOffset.y > self.offsetY && scrollVIEw.contentOffset.y > 0) {//向上滑动//按钮消失[UIVIEw TransitionWithVIEw:self.bottombutton duration:0.1 options:UIVIEwAnimationoptionTransitionNone animations:^{self.bottombutton.frame = CGRectMake(kScreenW - 80,kScreenH - 65,60);} completion:NulL]; }else if (scrollVIEw.contentOffset.y < self.offsetY ){//向下滑动//按钮出现[UIVIEw TransitionWithVIEw:self.bottombutton duration:0.1 options:UIVIEwAnimationoptionTransitionNone animations:^{self.bottombutton.frame = CGRectMake(kScreenW - 80,60);} completion:NulL];}self.offsetY = scrollVIEw.contentOffset.y;//将当前位移变成缓存位移}

以上所述是小编给大家介绍的AndroID开发中在tableVIEw上添加悬浮按钮的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的Android开发中在TableView上添加悬浮按钮的方法全部内容,希望文章能够帮你解决Android开发中在TableView上添加悬浮按钮的方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1147783.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存