Android開(kāi)發(fā)中在TableView上添加懸浮按鈕的方法
如果直接在TableVIewController上貼Button的話會(huì)導(dǎo)致這個(gè)會(huì)隨之滾動(dòng),下面解決在TableView上實(shí)現(xiàn)位置固定懸浮按鈕的兩種方法:
1.在view上貼tableView,然后將懸浮按鈕貼在view的最頂層
2.使用window
首先看一下最終的效果,在tableViewController上添加一個(gè)懸浮按鈕,該按鈕不能隨著視圖的滾動(dòng)而滾動(dòng)

首先介紹上面的第一種方法:
1)創(chuàng)建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)創(chuàng)建屬性到最頂部
@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)實(shí)現(xiàn)按鈕事件
- (void)onTapLiveBtn
{
NSLog(@"點(diǎn)擊底部按鈕");
}
接下來(lái)介紹第二種方法:
1)創(chuàng)建一個(gè)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)創(chuàng)建window和button
默認(rèn)的情況下系統(tǒng)只有一個(gè)window這時(shí)我們需要設(shè)置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];//關(guān)鍵語(yǔ)句,顯示window
}
3)延時(shí)加載window,注意我們需要在rootWindow創(chuàng)建完成之后再創(chuàng)建這個(gè)懸浮的按鈕
- (void)viewDidLoad {
[super viewDidLoad];
[self performSelector:@selector(createButton) withObject:nil afterDelay:1];
}
4)實(shí)現(xiàn)按鈕事件
- (void)onTapLiveBtn
{
NSLog(@"點(diǎn)擊底部按鈕");
}
注意::最后再添加一個(gè)小功能,使tableview上下滑動(dòng)的時(shí)候,按鈕動(dòng)畫(huà)效果的出現(xiàn)和消失,在這里是上拉消失,下拽出現(xiàn)
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView.contentOffset.y > self.offsetY && scrollView.contentOffset.y > 0) {//向上滑動(dòng)
//按鈕消失
[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 ){//向下滑動(dòng)
//按鈕出現(xiàn)
[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;//將當(dāng)前位移變成緩存位移
}
以上所述是小編給大家介紹的Android開(kāi)發(fā)中在TableView上添加懸浮按鈕的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android自定義可拖拽的懸浮按鈕DragFloatingActionButton
- Android中FloatingActionButton實(shí)現(xiàn)懸浮按鈕實(shí)例
- Android實(shí)現(xiàn)系統(tǒng)級(jí)懸浮按鈕
- Android懸浮按鈕點(diǎn)擊返回頂部FloatingActionButton
- Android開(kāi)發(fā)模仿qq視頻通話懸浮按鈕(實(shí)例代碼)
- Android利用WindowManager生成懸浮按鈕及懸浮菜單
- Android開(kāi)發(fā)懸浮按鈕 Floating ActionButton的實(shí)現(xiàn)方法
- Android利用懸浮按鈕實(shí)現(xiàn)翻頁(yè)效果
- 圣誕節(jié),寫(xiě)個(gè)程序練練手————Android 全界面懸浮按鈕實(shí)現(xiàn)
- Android懸浮按鈕的使用方法
相關(guān)文章
Android 個(gè)人理財(cái)工具六:顯示賬單明細(xì) 下
本文主要節(jié)誒是Android 個(gè)人理財(cái)工具顯示賬單明細(xì),主要實(shí)現(xiàn)此窗口的查詢和刪除功能,這里提供實(shí)現(xiàn)代碼,有興趣的小伙伴可以參考下2016-08-08
Android style的繼承方式 點(diǎn)(.)和parent詳解及實(shí)例
這篇文章主要介紹了Android style的繼承方式 點(diǎn)(.)和parent詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02
Android編程實(shí)現(xiàn)3D旋轉(zhuǎn)效果實(shí)例
這篇文章主要介紹了Android編程實(shí)現(xiàn)3D旋轉(zhuǎn)效果的方法,基于Android的Camera類實(shí)現(xiàn)坐標(biāo)變換達(dá)到圖片3D旋轉(zhuǎn)效果,需要的朋友可以參考下2016-01-01
Android中簡(jiǎn)單的電話管理與短信管理App編寫(xiě)實(shí)例
這篇文章主要介紹了Android中簡(jiǎn)單的電話管理與短信管理App編寫(xiě)實(shí)例,包括監(jiān)聽(tīng)電話的呼叫狀態(tài)以及短信群發(fā)聯(lián)系人選擇等基本功能的實(shí)現(xiàn),代碼突出要點(diǎn),需要的朋友可以參考下2016-04-04
Android Studio實(shí)現(xiàn)發(fā)短信功能
這篇文章主要介紹了Android Studio實(shí)現(xiàn)發(fā)短信功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-06-06
Android Handler移除Message詳解及實(shí)例代碼
這篇文章主要介紹了Android Handler移除Message詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02
Android WebView自定義長(zhǎng)按選擇實(shí)現(xiàn)收藏/分享選中文本功能
這篇文章主要介紹了Android WebView自定義長(zhǎng)按選擇實(shí)現(xiàn)收藏/分享選中文本功能,需要的朋友可以參考下2017-06-06
Android 下載并打開(kāi)PDF,Doc,Dwg文檔實(shí)例
本篇文章主要介紹了Android 下載并打開(kāi)PDF,Doc,Dwg文檔實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-04-04

