?版權聲明:本文為Andy_wangpeng****
『原創文章』****
,未經允許不得轉載。
如需轉載請注明版權
序章##
? ? ? ? 之前項目中遇到了一個彈出tableView列表
的需求,本能的就隨手寫了一個感覺很丑,然后看到了安卓的彈出控件Spinner
還不錯,可以根據內容隨意調整大小。然后心血來潮,就仿寫了一個,自己加了點效果。效果圖:
QQ20170630-221322-HD.gif
實現思路##
1.首先肯定是一個tableView加在Windows上,這個tableView 上方一個View遮蓋。下方有個View包含『確定』
與『取消』
兩個按鈕。然后點擊背景遮蓋,也可以讓其消失,總體實現很簡單。
在這里利用了下UIControl,當做遮蓋,給其添加了一個點擊事件。點擊即可消失
UIControl: 【UIControl派生自UIView類,所以每個控件都有很多視圖的特性,包括附著于其他視圖的能力。所有控件都擁有一套共同的屬性和方法】。
下面給出部分代碼,為什么呢?有些人一味索取,還嫌棄這嫌棄那,也為了大家動手能力、以及思考能力,關鍵代碼已經給出。望大家有興趣的研究研究。還是那句話****不喜勿噴****。。。
一、初始化控件(UI)
//1.創建tableview
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.dataSource = self;
_tableView.delegate = self;
[self addSubview:_tableView];
//2.列表標題
_titleView = [[UILabel alloc] initWithFrame:CGRectZero];
_titleView.font = [UIFont systemFontOfSize:17.0f];
_titleView.backgroundColor = NAVCOLOR;
_titleView.text = @"養戶列表";
_titleView.textAlignment = NSTextAlignmentCenter;
_titleView.textColor = [UIColor whiteColor];
[self addSubview:_titleView];
//3.底部view按鈕
UIView *bottomView = [[UIView alloc] init];
bottomView.backgroundColor = [UIColor whiteColor];
[self addSubview:bottomView];
self.bottomView = bottomView;
//3.1按鈕
_okBtn = [[UIButton alloc] initWithFrame:CGRectZero];
[_okBtn setTitle:@"確定" forState:UIControlStateNormal];
_okBtn.titleLabel.font = [UIFont systemFontOfSize:13];
[_okBtn setTitleColor:RGBA(77, 173, 74, 1) forState:UIControlStateNormal];
_okBtn.backgroundColor = [UIColor whiteColor];
[_okBtn addTarget:self action:@selector(checkItem) forControlEvents:UIControlEventTouchUpInside];
[bottomView addSubview:_okBtn];
//3.2按鈕
_cancelBtn = [[UIButton alloc] initWithFrame:CGRectZero];
[_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
_cancelBtn.titleLabel.font = [UIFont systemFontOfSize:13];
[_cancelBtn setTitleColor:RGBA(77, 173, 74, 1) forState:UIControlStateNormal];
_cancelBtn.backgroundColor = [UIColor whiteColor];
[_cancelBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
[bottomView addSubview:_cancelBtn];
//0.遮蓋View
UIControl *coverView = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
coverView.backgroundColor = [UIColor colorWithRed:.16 green:.17 blue:.21 alpha:.5];
[coverView addTarget:self
action:@selector(dismiss)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:coverView];
self.coverView = coverView;
二、展示 與 消失
#pragma mark - show & dismiss
- (void)show
{
UIWindow *keywindow = [[UIApplication sharedApplication] windows][0];
[keywindow addSubview:self.coverView];
[keywindow addSubview:self];
[self.tableView reloadData];
// 設置布局
[self uploadLayout];
// 漸出動畫
[self fadeIn];
}
- (void)dismiss
{
[self fadeOut];
}
三、布局與動畫
#pragma mark - uploadLayout
//獲取tableview 高度
-(float)getTableViewHeight
{
[self.tableView layoutIfNeeded];
return self.tableView.contentSize.height;
}
-(void)uploadLayout{
//動態高度調整
if ([self getTableViewHeight] >= SCREEN_HEIGHT- 80) {
CGSize winSize = [UIScreen mainScreen].bounds.size;
self.tableView.frame = CGRectMake((winSize.width-kCenterPopupListViewWidth)/2.0f,
(winSize.height - kCenterPopupListViewHeight)/2.0,
kCenterPopupListViewWidth,
kCenterPopupListViewHeight);
}else{
CGSize winSize = [UIScreen mainScreen].bounds.size;
self.tableView.frame = CGRectMake((winSize.width-kCenterPopupListViewWidth)/2.0f,
(winSize.height - [self getTableViewHeight])/2.0,
kCenterPopupListViewWidth,
[self getTableViewHeight]);
}
//標題frame
_titleView.frame = CGRectMake(self.tableView.left, self.tableView.top-30, self.tableView.width, 30);
//底部按鈕
self.bottomView.frame = CGRectMake(self.tableView.left, self.tableView.bottom, self.tableView.width, 44);
//_titleView && 圓角
UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:_titleView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(20, 20)];
CAShapeLayer* shape = [[CAShapeLayer alloc] init];
[shape setPath:rounded.CGPath];
_titleView.layer.mask = shape;
_titleView.layer.masksToBounds = YES;
//確定 && 取消按鈕
CGFloat cancelBtnW = 90;
_okBtn.frame = CGRectMake(self.bottomView.width - cancelBtnW , 0, cancelBtnW, self.bottomView.height);
_cancelBtn.frame = CGRectMake(_okBtn.left - cancelBtnW , 0, cancelBtnW, self.bottomView.height);
}
#pragma mark - animations(動畫)
- (void)fadeIn
{
self.transform = CGAffineTransformMakeScale(1.3, 1.3);
self.alpha = 0;
[UIView animateWithDuration:.35 animations:^{
self.alpha = 1;
self.transform = CGAffineTransformMakeScale(1, 1);
}];
}
- (void)fadeOut
{
[UIView animateWithDuration:.35 animations:^{
self.transform = CGAffineTransformMakeScale(1.3, 1.3);
self.alpha = 0.0;
} completion:^(BOOL finished) {
if (finished) {
[self.coverView removeFromSuperview];
[self removeFromSuperview];
}
}];
}
#//一定要寫(雖然不知道為什么,但是不寫,點擊事件失效)
#define mark - UITouch
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self dismiss];
}