国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android開發中在TableView上添加懸浮按鈕的方法

2019-12-12 04:42:44
字體:
來源:轉載
供稿:網友

如果直接在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上添加懸浮按鈕的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 甘洛县| 明光市| 务川| 丹江口市| 柳江县| 昌宁县| 邯郸县| 西乌珠穆沁旗| 沈阳市| 定远县| 威远县| 北宁市| 邳州市| 上林县| 龙游县| 青浦区| 江城| 台南县| 桂阳县| 巴彦淖尔市| 许昌县| 积石山| 平远县| 鹤庆县| 丽水市| 巴中市| 永德县| 抚宁县| 特克斯县| 句容市| 夏邑县| 进贤县| 珠海市| 息烽县| 临武县| 普兰县| 定州市| 含山县| 玉环县| 瑞昌市| 新和县|