iOS - 自定义 下弹框
2017-04-26 本文已影响121人
Mr_Bob_
具体封装代码
#pragma mark -- 懒加载
- (UIView *)popTopView {
if (!_popTopView) {
// 自定义弹框内容
_popTopView = [[UIView alloc] initWithFrame:CGRectMake(0, -SCREEN_HEIGHT_NO_STATUS / 2.0f, SCREEN_WIDTH_NO_STATUS, SCREEN_HEIGHT_NO_STATUS / 2.0f)];
_popTopView.backgroundColor = [UIColor whiteColor];
// 自定义弹框内容
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64.0f)];
titleView.backgroundColor = MAIN_TINT_COLOR;
[_popTopView addSubview:titleView];
UILabel *topTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 44.0f)];
topTitleLabel.font = [UIFont systemFontOfSize:20.0];
topTitleLabel.textAlignment = NSTextAlignmentCenter;
topTitleLabel.textColor = [UIColor whiteColor];
topTitleLabel.backgroundColor = [UIColor clearColor];
topTitleLabel.text = @"切换工作室";
[titleView addSubview:topTitleLabel];
}
return _popTopView;
}
- (UIView *)maskView {
if (!_maskView) {
_maskView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
_maskView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.400];
_maskView.alpha = 0.0f;
// 添加点击背景按钮
UIButton *btn = [[UIButton alloc] initWithFrame:[UIScreen mainScreen].bounds];
[btn addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
[_maskView addSubview:btn];
}
return _maskView;
}
- (void)openPopTopViewAction{ // 打开下拉弹框
[[UIApplication sharedApplication].keyWindow addSubview:self.maskView];
[[UIApplication sharedApplication].keyWindow addSubview:self.popTopView];
[UIView animateWithDuration:0.25 animations:^{
self.maskView.alpha = 1.0;
self.popTopView.transform = CGAffineTransformTranslate(self.popTopView.transform, 0, SCREEN_HEIGHT_NO_STATUS / 2.0f);
} completion:^(BOOL finished) {
NSLog(@"%s", __func__);
}];
}
- (void)close {
// 关闭顶部视图动画
[UIView animateWithDuration:0.3 animations:^{
self.maskView.alpha = 0.0;
self.popTopView.transform = CGAffineTransformIdentity;
}completion:^(BOOL finished) {
[self.maskView removeFromSuperview];
[self.popTopView removeFromSuperview];
}];
}
具体的调用方法:
直接调用打开下拉弹框方法
[self openPopTopViewAction];
或者调用关闭弹框方法
[self close];