©版权声明:本文为Andy_wangpeng****
『原创文章』****
,未经允许不得转载。
如需转载请注明版权
序章##
之前项目中遇到了一个弹出tableView列表
的需求,本能的就随手写了一个感觉很丑,然后看到了安卓的弹出控件Spinner
还不错,可以根据内容随意调整大小。然后心血来潮,就仿写了一个,自己加了点效果。效果图:
实现思路##
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];
}