因为项目需求,自定义弹出视图的基础上加一些用户体验更佳的动画和友情文字以及图片提示等,就有了写了这篇博客,一为方便自己以后在其他项目中复用二为更多开发者拷贝使用,若有书写代码不足之处欢迎批评指正。
先看下我的Demo示例运行效果图如下:
实现文件只有一个类:(QDTitleAlertView.h 和 QDTitleAlertView.m)
1. QDTitleAlertView.h头文件代码如下:
#import <UIKit/UIKit.h>
@interface QDTitleAlertView : UIView
//设置遮罩蒙板响应事件是否关闭
@property (nonatomic, assign) BOOL closeUserInteractionEnabled;
@property (nonatomic,copy) void(^confirmButtonBlock)();
@property (nonatomic,copy) void(^cancelButtonBlock)();
//初始化
+ (instancetype)alertWithFrame:(CGRect)frame title:(NSString *)title confirmStr:(NSString *)confirmStr cancelStr:(NSString *)cancelStr;
//弹窗
- (void)alert;
@end
2. QDTitleAlertView.m实现文件代码如下:
#import "QDTitleAlertView.h"
const CGFloat titleAlertViewWidthRatio = 0.655; //宽度系数
const CGFloat titleAlertViewHeightRatio = 0.206; //高度系统
@interface QDTitleAlertView ()
@property (nonatomic, weak) UIView *bgView;
@property (nonatomic, weak) UIImageView *bgImageView;
@property (nonatomic, weak) UILabel *titleLabel;//标题
@property (nonatomic, weak) UIView *horizontalLineV;//水平线
@property (nonatomic, weak) UIView *verticalLineV;//垂直线
@property (nonatomic, weak) UIButton *confirmButton;//确认按钮
@property (nonatomic, weak) UIButton *cancleButton;//取消按钮
@property (nonatomic, copy) NSString *title;//标题
@property (nonatomic, copy) NSString *confirmStr;//确认按钮
@property (nonatomic, copy) NSString *cancelStr;//取消按钮
@end
@implementation QDTitleAlertView
+ (instancetype)alertWithFrame:(CGRect)frame title:(NSString *)title confirmStr:(NSString *)confirmStr cancelStr:(NSString *)cancelStr{
QDTitleAlertView *alert = [[self alloc] initWithFrame:frame];
alert.title = title;
alert.confirmStr = confirmStr;
alert.cancelStr = cancelStr;
return alert;
}
- (void)alert{
[QDKeyWindow addSubview:self];
}
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self setUpUI];
}
return self;
}
- (void)setUpUI{
//半透明遮盖视图(满屏)
UIView *bgView = [[UIView alloc] initWithFrame:UIApplication.sharedApplication.keyWindow.bounds];
bgView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
bgView.alpha =0;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgViewTapped)];
[bgView addGestureRecognizer:tap];
[self addSubview:bgView];
self.bgView = bgView;
//背景图片视图
UIImageView *bgImageView = [[UIImageView alloc] init];
bgImageView.backgroundColor = [UIColor whiteColor];
[bgImageView setUserInteractionEnabled:YES];
[bgImageView roundViewWithRadius:6];
[self addSubview:bgImageView];
self.bgImageView = bgImageView;
//标题
UILabel *titleL = [[UILabel alloc] init];
titleL.numberOfLines = 2;
titleL.textAlignment = NSTextAlignmentCenter;
titleL.font = QDFont16;
titleL.textColor = [UIColor blackColor];
[self.bgImageView addSubview:titleL];
self.titleLabel = titleL;
//横线
UIView *horizontalLineV = [[UIView alloc]init];
horizontalLineV.backgroundColor = [UIColor grayColor];
horizontalLineV.alpha = 0.5;
[self.bgImageView addSubview:horizontalLineV];
self.horizontalLineV = horizontalLineV;
//竖线
UIView *verticalLineV = [[UIView alloc] init];
verticalLineV.backgroundColor = [UIColor grayColor];
verticalLineV.alpha = 0.5;
[self.bgImageView addSubview:verticalLineV];
self.verticalLineV = verticalLineV;
//确定按钮
UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
[confirmButton setTitle:@"确定" forState:UIControlStateNormal];
[confirmButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[confirmButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
[confirmButton setBackgroundColor:[UIColor whiteColor]];
[confirmButton setBackgroundColor:QDColorButtonHighlight forState:UIControlStateHighlighted];
[confirmButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
[confirmButton addTarget:self action:@selector(confirmButtonTapped) forControlEvents:UIControlEventTouchUpInside];
[self.bgImageView addSubview:confirmButton];
self.confirmButton = confirmButton;
//取消按钮
UIButton *cancleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cancleButton setTitle:@"取消" forState:UIControlStateNormal];
[cancleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cancleButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
[cancleButton setBackgroundColor:[UIColor whiteColor]];
[cancleButton setBackgroundColor:QDColorButtonHighlight forState:UIControlStateHighlighted];
[cancleButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
[cancleButton addTarget:self action:@selector(cancleButtonTapped) forControlEvents:UIControlEventTouchUpInside];
self.cancleButton = cancleButton;
[self.bgImageView addSubview:cancleButton];
if (UIDevice.currentDevice.systemVersion.doubleValue < 9.0) {
[UIView animateWithDuration:0.2 animations:^{
self.bgView.alpha = 1;
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.fromValue = [NSNumber numberWithFloat:0.7] ;
scaleAnimation.toValue = [NSNumber numberWithFloat:1.0] ;
scaleAnimation.duration = 0.1;
scaleAnimation.autoreverses = NO;
[self.bgImageView.layer addAnimation:scaleAnimation forKey:@"transform.scale"];
}];
}else{
CASpringAnimation *scaleAnimation = [CASpringAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.damping = 5;
scaleAnimation.stiffness = 100;
scaleAnimation.mass = 0.35;
scaleAnimation.fromValue = [NSNumber numberWithFloat:0.7] ;
scaleAnimation.toValue = [NSNumber numberWithFloat:1.0] ;
scaleAnimation.duration = scaleAnimation.settlingDuration;
scaleAnimation.autoreverses = NO;
[self.bgImageView.layer addAnimation:scaleAnimation forKey:@"transform.scale"];
[UIView animateWithDuration:0.2 animations:^{
self.bgView.alpha = 1;
}];
}
}
- (void)layoutSubviews{
[super layoutSubviews];
//内容宽高
CGFloat contentW = [UIScreen mainScreen].bounds.size.width * titleAlertViewWidthRatio;
CGFloat contentH = [UIScreen mainScreen].bounds.size.height * titleAlertViewHeightRatio;
[self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(contentW, contentH));
make.center.mas_equalTo(self);
}];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.bgImageView);
make.top.mas_equalTo(self.bgImageView.mas_top).mas_offset(30);
make.left.mas_equalTo(self.bgImageView).mas_offset(20);
make.right.mas_equalTo(self.bgImageView.mas_right).mas_offset(-20);
}];
[self.horizontalLineV mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(0.5);
make.left.mas_equalTo(self.bgImageView);
make.right.mas_equalTo(self.bgImageView.mas_right);
make.bottom.equalTo(self.bgImageView.mas_bottom).mas_offset(-50);
}];
[self.verticalLineV mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(0.5, 50));
make.centerX.equalTo(self.bgImageView);
make.bottom.equalTo(self.bgImageView.mas_bottom);
}];
[self.cancleButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.bgImageView);
make.bottom.mas_equalTo(self.bgImageView.mas_bottom);
make.size.mas_equalTo(CGSizeMake(contentW/2 - 0.5, 49));
}];
[self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(self.bgImageView.mas_right);
make.bottom.mas_equalTo(self.bgImageView.mas_bottom);
make.size.mas_equalTo(CGSizeMake(contentW/2 - 0.5, 49));
}];
}
#pragma mark -按钮点击事件-
- (void)confirmButtonTapped{
if (self.confirmButtonBlock) {
self.confirmButtonBlock();
}
[UIView animateWithDuration:0.15 animations:^{
self.bgView.alpha = 0;
[self.bgImageView.layer removeAllAnimations];
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.fromValue = [NSNumber numberWithFloat:1.0] ;
scaleAnimation.toValue = [NSNumber numberWithFloat:0.5] ;
scaleAnimation.duration = 0.15;
scaleAnimation.autoreverses = NO;
[self.bgImageView.layer addAnimation:scaleAnimation forKey:@"transform.scale"];
}];
[self performSelector:@selector(removeSelf) withObject:nil afterDelay:0.12];
}
//点击遮罩
- (void)bgViewTapped{
if(!self.closeUserInteractionEnabled) [self cancleButtonTapped];
}
- (void)cancleButtonTapped{
if (self.cancelButtonBlock) {
self.cancelButtonBlock();
}
[UIView animateWithDuration:0.15 animations:^{
self.bgView.alpha = 0;
[self.bgImageView.layer removeAllAnimations];
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.fromValue = [NSNumber numberWithFloat:1.0] ;
scaleAnimation.toValue = [NSNumber numberWithFloat:0.5] ;
scaleAnimation.duration = 0.15;
scaleAnimation.autoreverses = NO;
[self.bgImageView.layer addAnimation:scaleAnimation forKey:@"transform.scale"];
}];
[self performSelector:@selector(removeSelf) withObject:nil afterDelay:0.12];
}
- (void)removeSelf{
[self removeFromSuperview];
}
- (void)setTitle:(NSString *)title{
_title = title;
self.titleLabel.text = title;
}
- (void)setConfirmStr:(NSString *)confirmStr{
_confirmStr = confirmStr;
if (confirmStr) {
[self.confirmButton setTitle:confirmStr forState:UIControlStateNormal];
}
}
- (void)setCancelStr:(NSString *)cancelStr{
_cancelStr = cancelStr;
if (cancelStr) {
[self.cancleButton setTitle:cancelStr forState:UIControlStateNormal];
}
}
@end
3.调用方式:
QDTitleAlertView *alertView = [QDTitleAlertView alertWithFrame:self.view.window.bounds title:@"重置将清除设置,确认重置?" confirmStr:@"确定" cancelStr:@"取消"];
alertView.confirmButtonBlock = ^{
};
[alertView alert];
注意:有的项目中有需求背景遮罩蒙板不让用户点击,只需要创建
QDTitleAlertView
对象之后,设置属性closeUserInteractionEnabled = YES
即可实现。
如果您希望在你的项目中直接使用,那就直接拷贝就行
如果觉得还不够具体,后期考虑将代码上传github与分享源码,希望深层交流的童鞋加我qq:1107160410,我们一起探讨,感谢您看到这里,如方便的话点个赞,我会更加努力。
- 欢迎点击下篇博客链接
//www.greatytc.com/p/abfdedaef047查看更加相关内容,可能会帮到您。