Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

TableView 懸浮按鈕實作:兩種讓按鈕不隨滾動的方案

編輯:關於 Android 程式設計

直接把 Button 貼在 TableViewController 上,按鈕會跟著列表一起滾走。要讓它固定在畫面上,有兩種常見解法:一是把 tableView 當普通子 View 貼在根 view 上,按鈕疊在最頂層;二是另外開一個 UIWindow 承載按鈕。

最終效果如下,按鈕固定在右下角,不隨視圖滾動:

方法一:層級覆蓋

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)在 viewDidLoad 中把兩者加入畫面,注意 tableView 高度要預留底部空間

 @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(@"點擊底部按鈕"); }

這種做法簡單直觀,缺點是按鈕被綁在單一頁面上,若有多個需要共用同一顆懸浮按鈕的場景,就得各自重複實作。

方法二:獨立 UIWindow

1)宣告 window 與 button 屬性。這裡務必用 strong,否則 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 與按鈕。系統預設只有一個 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(@"點擊底部按鈕"); }

加分功能:隨滾動方向淡入淡出

再補一個常見的互動細節——上滑時按鈕收起,下滑時出現。透過 scrollViewDidScroll 比對 contentOffset 與緩存位移即可判斷方向:

 -(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;//將當前位移變成緩存位移 }

兩種方案的取捨:方法一適合按鈕只服務單一頁面;方法二的好處是懸浮層獨立於任何 ViewController,跨頁面都能持續顯示,代價是要自己處理 window 的生命週期與記憶體管理(離開時別忘了 release)。

以上就是這次整理的兩種做法,若有任何疑問歡迎留言討論。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved