編輯:關於 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)。
以上就是這次整理的兩種做法,若有任何疑問歡迎留言討論。
Android 手勢識別實作:GestureOverlayView 與 GestureLibrary 用法
Android 的 android.gesture 套件提供了手勢繪製與識別能力。典型流程包含建立繪圖區、監聽手勢輸入、儲存手勢樣本,以及比對使用者新畫出的手勢。以下概念
深度改造 Android ListView:打造高效能瀑布流布局
這是 ListView 系列三部曲的最終篇。前兩篇我們已經從源碼層面透徹剖析了 ListView 的工作原理與常見問題,這一篇則要動手改造它,讓 ListView 能以瀑
華為麥芒5高配版值得加價嗎?標配版與高配版的三大差異
麥芒5的正面維持了較寬的黑邊設計,很「華為」的風格。真正讓人猶豫的是版本選擇:標配版 2399 元、高配版 2599 元,價差 200 元,到底多買到什麼?標配版與高配版
Android 入門:用程式碼動態生成 TableLayout 表格(下)
上一篇初步介紹了 TableLayout 的常用屬性,這次往前一步:如何用程式碼動態地往 TableLayout 裡塞資料。這在實務中很常見——資料是執行時才拿到的,沒辦