iOS弹框类封装的类方法

因为工作中经常用到,所以封装了一个透明的弹框,稍加改动后复用性还是极强的
.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被点击了");
    }];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,831评论 25 709
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,282评论 30 472
  • 1939年,邱吉尔曾说俄罗斯:“一个包裹在谜里面的谜”。而如今,对于me国来说也是一样。 在许多方面,...
    刘亚非阅读 339评论 0 0
  • 三月春寒问牡丹: 为啥飞雪占良田?翻档案,解封签,原来如此贿贪官!!
    木貞ma阅读 145评论 0 1
  • 世上的爱情有这么多,不知有多少是可以抵达这样的境界的:从爱上你的十七岁,到依然爱你的七十岁,心里念的,都是彼此的好...
    高桥先生阅读 347评论 0 0