iOS-悬浮按钮
程序员文章站
2022-06-28 11:38:20
在项目中可能会有这种需求,即在一个界面最顶层需要一个按钮,这个按钮可能是发布信息功能,也可能是回到顶部.这样我们可以使用uiwindow这个神奇的控件实现,很简单.
完整项目:
https://gi...
在项目中可能会有这种需求,即在一个界面最顶层需要一个按钮,这个按钮可能是发布信息功能,也可能是回到顶部.这样我们可以使用uiwindow这个神奇的控件实现,很简单.
完整项目:
https://github.com/qxuewei/xwsuspendbtn
最终实现效果如下:
实现逻辑:
1.在需要出现悬浮按钮的类中声明按钮uibutton属性和uiwindow属性喎? f/ware/vc/"="" target="_blank" class="keylink">vcd4ncjxwcmugy2xhc3m9"brush:java;">
/** window */
@property (nonatomic, strong) uiwindow *window;
/** 悬浮按钮 */
@property (nonatomic, strong) uibutton *button;
2.创建uiwindow以及悬浮按钮方法
-(void)creatsuspendbtn{ //悬浮按钮 _button = [uibutton buttonwithtype:uibuttontypecustom]; [_button setimage:[uiimage imagenamed:@"plus"] forstate:uicontrolstatenormal]; cgfloat screenwidth = [uiscreen mainscreen].bounds.size.width; cgfloat screenheight = [uiscreen mainscreen].bounds.size.height; _button.frame = cgrectmake(0,0, 64, 64); [_button addtarget:self action:@selector(suspendbtnclick) forcontrolevents:uicontroleventtouchupinside]; //悬浮按钮所处的顶端uiwindow _window = [[uiwindow alloc] initwithframe:cgrectmake(screenwidth*0.5-32, screenheight-84, 64, 64)]; //使得新建window在最顶端 _window.windowlevel = uiwindowlevelalert + 1; _window.backgroundcolor = [uicolor clearcolor]; [_window addsubview:_button]; //显示window [_window makekeyandvisible]; }
3.初始化视图时创建悬浮按钮
- (void)viewdidload { [super viewdidload]; // do any additional setup after loading the view, typically from a nib. [self.maintableview setdelegate:self]; [self.maintableview setdatasource:self]; //延时加载window,注意我们需要在rootwindow创建完成之后再创建这个悬浮的按钮 [self performselector:@selector(creatsuspendbtn) withobject:nil afterdelay:0.2]; }
项目github地址:
https://github.com/qxuewei/xwsuspendbtn
上一篇: HBase海量数据存储