欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2016年11月10日 17:20:38   作者:菁欣  
如果直接在TableVIewController上貼Button的話會導(dǎo)致這個會隨之滾動,下面通過本文給大家分享在TableView上實(shí)現(xiàn)位置固定懸浮按鈕的兩種方法,對tableview 懸浮按鈕感興趣的朋友一起學(xué)習(xí)吧

如果直接在TableVIewController上貼Button的話會導(dǎo)致這個會隨之滾動,下面解決在TableView上實(shí)現(xiàn)位置固定懸浮按鈕的兩種方法:

  1.在view上貼tableView,然后將懸浮按鈕貼在view的最頂層

  2.使用window

首先看一下最終的效果,在tableViewController上添加一個懸浮按鈕,該按鈕不能隨著視圖的滾動而滾動

首先介紹上面的第一種方法:

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

接下來介紹第二種方法:

1)創(chuàng)建一個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)只有一個window這時我們需要設(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)鍵語句,顯示window
}

3)延時加載window,注意我們需要在rootWindow創(chuàng)建完成之后再創(chuàng)建這個懸浮的按鈕

- (void)viewDidLoad {
[super viewDidLoad];
[self performSelector:@selector(createButton) withObject:nil afterDelay:1]; 
}

4)實(shí)現(xiàn)按鈕事件

- (void)onTapLiveBtn
{
NSLog(@"點(diǎn)擊底部按鈕");
}

注意::最后再添加一個小功能,使tableview上下滑動的時候,按鈕動畫效果的出現(xiàn)和消失,在這里是上拉消失,下拽出現(xiàn)

-(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 ){//向下滑動
//按鈕出現(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開發(fā)中在TableView上添加懸浮按鈕的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論