iOS-自定义alertView(非常实用)
2019-04-02 本文已影响0人
记忆的北极
@interface AlertModel()
{
UIView * _popTopView;
UIView * _maskView;
}
@end
@implementation AlertModel
#pragma mark -- 懒加载
- (UIView *)popTopView {
if (!_popTopView) {
// 自定义弹框内容
_popTopView = [[UIView alloc] initWithFrame:CGRectMake(10, 200, SCREEN_WIDTH-20, 300)];
_popTopView.backgroundColor = [UIColor whiteColor];
// 自定义弹框内容
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64.0f)];
titleView.backgroundColor = [UIColor lightGrayColor];
[_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];
}];
}
@end