因为工作中经常用到,所以封装了一个透明的弹框,稍加改动后复用性还是极强的
.h文件中:
#import <UIKit/UIKit.h>
@interface AlertView : UIView
@property (nonatomic,copy) void(^confirmButtonAction)(int);
+(void)configureAlertViewWithMessage:(NSString *)message ImageName:(NSString *)imageName ConfirmButtonAction:(void(^)())confirmButtonAction ;
@end
.m文件中
#import "AlertView.h"
#import <Masonry.h>
@implementation AlertView
+(void)configureAlertViewWithMessage:(NSString *)message ImageName:(NSString *)imageName ConfirmButtonAction:(void(^)())confirmButtonAction {
//1.先创建自身,加到window
AlertView *view = [AlertView new];
view.confirmButtonAction = confirmButtonAction; ///
UIWindow *window = [UIApplication sharedApplication].keyWindow;
[window addSubview: view];
//2.创建屏幕整个遮罩 ,加到自身
UIView *maskClearView = [[UIView alloc] init];
maskClearView.backgroundColor = [UIColor clearColor];
[view addSubview:maskClearView];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(window);
}];
[maskClearView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(view);
}];
//3.创建视图添加到遮罩上
UIView *alertGrayView = [[UIView alloc] init];
alertGrayView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
alertGrayView.layer.cornerRadius = 12.0f;
[maskClearView addSubview:alertGrayView];
[alertGrayView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(155);
make.height.mas_equalTo(250);
make.center.equalTo(maskClearView);
}];
//4.依次往视图上面添加(子视图,控件等)
UIImageView *errorImgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 47, 47)];
errorImgV.image = [UIImage imageNamed:imageName];
errorImgV.contentMode = UIViewContentModeScaleAspectFill;
[alertGrayView addSubview:errorImgV];
[errorImgV mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(alertGrayView);
make.top.mas_equalTo(alertGrayView).mas_offset(32);
}];
UILabel *messageLbl = [[UILabel alloc]init];
[messageLbl setFont:[UIFont fontWithName:@"PingFangSC-Light" size:15.0f]];
[messageLbl setTextColor:[UIColor whiteColor]];
messageLbl.textAlignment = NSTextAlignmentCenter;
messageLbl.numberOfLines = 0;
[alertGrayView addSubview:messageLbl];
messageLbl.text = message;
[messageLbl mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(alertGrayView);
make.centerY.mas_equalTo(errorImgV).mas_offset(60);
make.width.mas_equalTo(140);
}];
UIButton *confirmButton = [[UIButton alloc] init];
[confirmButton setTitle:@"确定" forState:UIControlStateNormal];
[confirmButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[confirmButton.titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
[confirmButton.titleLabel setFont:[UIFont monospacedDigitSystemFontOfSize:16 weight:1.78]];
[confirmButton setBackgroundColor:[UIColor whiteColor]];
confirmButton.layer.cornerRadius = 4;
[alertGrayView addSubview:confirmButton];
[confirmButton addTarget:view action:@selector(confirmButtonClick) forControlEvents:UIControlEventTouchUpInside];
[confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(alertGrayView);
make.bottom.equalTo(alertGrayView);
make.size.mas_equalTo(CGSizeMake(100, 30));
}];
//动画效果
[view animation];
}
- (void)confirmButtonClick {
//移除时,只需要移除自身
[self removeFromSuperview];
NSLog(@"点击了确定按钮");
if (_confirmButtonAction) {
_confirmButtonAction();
}
}
- (void)animation{
CAKeyframeAnimation *popAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
popAnimation.duration = 0.4;
popAnimation.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.01f, 0.01f, 1.0f)],
[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1f, 1.1f, 1.0f)],
[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9f, 0.9f, 1.0f)],
[NSValue valueWithCATransform3D:CATransform3DIdentity]];
popAnimation.keyTimes = @[@0.0f, @0.5f, @0.75f, @1.0f];
popAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[self.layer addAnimation:popAnimation forKey:nil];
}
@end
最后,在需要调用的地方,可以直接调用:
[AlertView configureAlertViewWithMessage:@"Created by 邓昊 on 2017/5/21.Copyright © 2017年 贼の疯狂oо. All rights reserved." ImageName:@"oxford_play" ConfirmButtonAction:^{
NSLog(@"confirmButton被点击了");
}];