iOS彈框類(lèi)封裝的類(lèi)方法

因?yàn)楣ぷ髦薪?jīng)常用到,所以封裝了一個(gè)透明的彈框,稍加改動(dòng)后復(fù)用性還是極強(qiáng)的
.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.先創(chuàng)建自身,加到window
    AlertView *view = [AlertView new];
    view.confirmButtonAction = confirmButtonAction;  ///
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview: view];
    
    //2.創(chuàng)建屏幕整個(gè)遮罩 ,加到自身
    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.創(chuàng)建視圖添加到遮罩上
    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));
    }];

    //動(dòng)畫(huà)效果
    [view animation];
    
}

- (void)confirmButtonClick {
    //移除時(shí),只需要移除自身
    [self removeFromSuperview];
    NSLog(@"點(diǎn)擊了確定按鈕");
    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

最后,在需要調(diào)用的地方,可以直接調(diào)用:

[AlertView configureAlertViewWithMessage:@"Created by 鄧昊 on 2017/5/21.Copyright ? 2017年 賊の瘋狂oо. All rights reserved." ImageName:@"oxford_play" ConfirmButtonAction:^{
        NSLog(@"confirmButton被點(diǎn)擊了");
    }];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,366評(píng)論 25 708
  • *面試心聲:其實(shí)這些題本人都沒(méi)怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個(gè)offer,總結(jié)起來(lái)就是把...
    Dove_iOS閱讀 27,667評(píng)論 30 472
  • 1939年,邱吉爾曾說(shuō)俄羅斯:“一個(gè)包裹在謎里面的謎”。而如今,對(duì)于me國(guó)來(lái)說(shuō)也是一樣。 在許多方面,...
    劉亞非閱讀 373評(píng)論 0 0
  • 三月春寒問(wèn)牡丹: 為啥飛雪占良田?翻檔案,解封簽,原來(lái)如此賄貪官??!
    木貞ma閱讀 160評(píng)論 0 1
  • 世上的愛(ài)情有這么多,不知有多少是可以抵達(dá)這樣的境界的:從愛(ài)上你的十七歲,到依然愛(ài)你的七十歲,心里念的,都是彼此的好...
    高橋先生閱讀 377評(píng)論 0 0

友情鏈接更多精彩內(nèi)容