高仿蘋果(UIAlertView)彈窗帶動(dòng)畫(1)

因?yàn)轫?xiàng)目需求,自定義彈出視圖的基礎(chǔ)上加一些用戶體驗(yàn)更佳的動(dòng)畫和友情文字以及圖片提示等,就有了寫了這篇博客,一為方便自己以后在其他項(xiàng)目中復(fù)用二為更多開發(fā)者拷貝使用,若有書寫代碼不足之處歡迎批評(píng)指正。

先看下我的Demo示例運(yùn)行效果圖如下:

彈窗動(dòng)態(tài)效果圖.gif

實(shí)現(xiàn)文件只有一個(gè)類:(QDTitleAlertView.h 和 QDTitleAlertView.m)

1. QDTitleAlertView.h頭文件代碼如下:

#import <UIKit/UIKit.h>

@interface QDTitleAlertView : UIView

//設(shè)置遮罩蒙板響應(yīng)事件是否關(guān)閉
@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實(shí)現(xiàn)文件代碼如下:

#import "QDTitleAlertView.h"

const CGFloat titleAlertViewWidthRatio = 0.655;  //寬度系數(shù)
const CGFloat titleAlertViewHeightRatio = 0.206; //高度系統(tǒng)

@interface QDTitleAlertView ()
@property (nonatomic, weak) UIView *bgView;
@property (nonatomic, weak) UIImageView *bgImageView;
@property (nonatomic, weak) UILabel *titleLabel;//標(biāo)題
@property (nonatomic, weak) UIView *horizontalLineV;//水平線
@property (nonatomic, weak) UIView *verticalLineV;//垂直線
@property (nonatomic, weak) UIButton *confirmButton;//確認(rèn)按鈕
@property (nonatomic, weak) UIButton *cancleButton;//取消按鈕

@property (nonatomic, copy) NSString *title;//標(biāo)題
@property (nonatomic, copy) NSString *confirmStr;//確認(rèn)按鈕
@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;
    
    //標(biāo)題
    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];
    
    //內(nèi)容寬高
    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 -按鈕點(diǎn)擊事件-
- (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];
}

//點(diǎn)擊遮罩
- (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.調(diào)用方式:

QDTitleAlertView *alertView = [QDTitleAlertView alertWithFrame:self.view.window.bounds title:@"重置將清除設(shè)置,確認(rèn)重置?" confirmStr:@"確定" cancelStr:@"取消"];
    alertView.confirmButtonBlock = ^{
        
    };
    [alertView alert];

注意:有的項(xiàng)目中有需求背景遮罩蒙板不讓用戶點(diǎn)擊,只需要?jiǎng)?chuàng)建QDTitleAlertView對(duì)象之后,設(shè)置屬性closeUserInteractionEnabled = YES即可實(shí)現(xiàn)。

如果您希望在你的項(xiàng)目中直接使用,那就直接拷貝就行
如果覺得還不夠具體,后期考慮將代碼上傳github與分享源碼,希望深層交流的童鞋加我qq:1107160410,我們一起探討,感謝您看到這里,如方便的話點(diǎn)個(gè)贊,我會(huì)更加努力。

最后編輯于
?著作權(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)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,063評(píng)論 25 709
  • 因?yàn)轫?xiàng)目需求,自定義彈出視圖的基礎(chǔ)上加一些用戶體驗(yàn)更佳的動(dòng)畫和友情文字以及圖片提示等,就有了寫了這篇博客,一為方便...
    硅谷干貨閱讀 741評(píng)論 0 0
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,569評(píng)論 19 139
  • 因?yàn)轫?xiàng)目需求,自定義彈出視圖的基礎(chǔ)上加一些用戶體驗(yàn)更佳的動(dòng)畫和友情文字以及圖片提示等,就有了寫了這篇博客,一為方便...
    硅谷干貨閱讀 1,136評(píng)論 0 0
  • 視覺
    思有幾憂閱讀 118評(píng)論 0 0

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