具體封裝代碼
#pragma mark -- 懶加載
- (UIView *)popTopView {
if (!_popTopView) {
// 自定義彈框內(nèi)容
_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];
// 自定義彈框內(nèi)容
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;
// 添加點(diǎn)擊背景按鈕
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 {
// 關(guān)閉頂部視圖動(dòng)畫
[UIView animateWithDuration:0.3 animations:^{
self.maskView.alpha = 0.0;
self.popTopView.transform = CGAffineTransformIdentity;
}completion:^(BOOL finished) {
[self.maskView removeFromSuperview];
[self.popTopView removeFromSuperview];
}];
}
具體的調(diào)用方法:
直接調(diào)用打開下拉彈框方法
[self openPopTopViewAction];
或者調(diào)用關(guān)閉彈框方法
[self close];