当前位置: 移动技术网 > IT编程>移动开发>Android > Android开发中在TableView上添加悬浮按钮的方法

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

3294小游戏,淘人品,全套服务

如果直接在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, 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, 0, 60, 60);
[_button addtarget:self action:@selector(ontaplivebtn) forcontrolevents:uicontroleventtouchupinside];
_window = [[uiwindow alloc]initwithframe: cgrectmake(kscreenw - 80, kscreenh - 80, 60, 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, 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, kscreenh - 140, 60, 60);
} completion:null];
}
self.offsety = scrollview.contentoffset.y;//将当前位移变成缓存位移
}

以上所述是小编给大家介绍的android开发中在tableview上添加悬浮按钮的方法,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网