自定义AlertView

(一)动画支持IOS7及以上

#import <UIKit/UIKit.h>

@protocol ITTAlertViewDelegate <NSObject>

@optional
- (void)clickLeftButtonDelegate;
- (void)clickRightButtonDelegate;

@end


@interface ITTAlertView : UIView

@property (nonatomic,strong) id<ITTAlertViewDelegate> delegate;

- (instancetype)initWithFrame:(CGRect)frame Title:(NSString *)title content:(NSString *)contentString leftButtonTitle:(NSString *)leftButtonTitle rightButtonTitle:(NSString *)rightButtonTitle;

- (void)showAlertView;

@end


(二)有弹跳动画,按钮有阴影

#import "ITTAlertView.h"


#define kAlertWidth 200
#define kAlertHeight 200


@interface ITTAlertView ()
{
    UIView *alertView;
    CGRect mainFrame;
}

@property (nonatomic,strong) UIView *backImageView;

@end

@implementation ITTAlertView


#define kTitleYOffset 15.0f
#define kTitleHeight 25.0f
#define kContentOffset 30.0f
#define kBetweenLabelOffset 20.0f


- (instancetype)initWithFrame:(CGRect)frame Title:(NSString *)title content:(NSString *)contentString leftButtonTitle:(NSString *)leftButtonTitle rightButtonTitle:(NSString *)rightButtonTitle

{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        [self initAlertViewFrame];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kTitleYOffset, kContentOffset, kAlertWidth - 2 * kTitleYOffset, kTitleHeight)];
        titleLabel.text = title;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        
        [alertView addSubview:titleLabel];
        
        
        UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(kTitleYOffset, kContentOffset + kTitleHeight, kAlertWidth - 2 * kTitleYOffset, kTitleHeight)];
        contentLabel.text = contentString;
        contentLabel.textAlignment = NSTextAlignmentCenter;
        
        [alertView addSubview:contentLabel];
        
        if (leftButtonTitle) {
            
            CGFloat buttonWidth = (kAlertWidth - 2 * kTitleYOffset - kBetweenLabelOffset) / 2;
            UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(kTitleYOffset, kContentOffset + 3 * kTitleHeight, buttonWidth, 30.0f)];
            [leftButton setTitle:leftButtonTitle forState:UIControlStateNormal];
            [leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [leftButton setBackgroundColor:[UIColor blueColor]];
            [leftButton addTarget:self action:@selector(clickLeftButton) forControlEvents:UIControlEventTouchUpInside];
            
            [self setShadowStyle:leftButton];
            [alertView addSubview:leftButton];
            
            
            UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(kTitleYOffset, kContentOffset + buttonWidth + kBetweenLabelOffset, buttonWidth, 30.0f)];
            [rightButton setTitle:rightButtonTitle forState:UIControlStateNormal];
            [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [rightButton setBackgroundColor:[UIColor blueColor]];
            [rightButton addTarget:self action:@selector(clickRightButton) forControlEvents:UIControlEventTouchUpInside];
            
            [self setShadowStyle:rightButton];
            [alertView addSubview:rightButton];
        }
        
        else {
            
            UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(kTitleYOffset, kContentOffset + 3 * kTitleHeight, kAlertWidth - 2 * kTitleYOffset, 30.0f)];
            
            [rightButton setTitle:rightButtonTitle forState:UIControlStateNormal];
            [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [rightButton setBackgroundColor:[UIColor blueColor]];
            [rightButton addTarget:self action:@selector(clickRightButton) forControlEvents:UIControlEventTouchUpInside];
            
            [self setShadowStyle:rightButton];
            [alertView addSubview:rightButton];
        }
    }
    
    return self;
}


- (void)initAlertViewFrame {
    
    self.frame = [self mainScreenFrame];
    self.opaque = YES;
    self.backgroundColor = [UIColor clearColor];
    
    [self makeBackgroundView];
    [self makeAlertPopupView];
    [self moveAlertPopupView];
}

- (void)makeBackgroundView {
    
    self.backImageView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.backImageView.backgroundColor = [UIColor blackColor];
    self.backImageView.alpha = 0.6f;
    
    [self addSubview:self.backImageView];
}


- (void)showAlertView {
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    [self showAlertView4Animate];
}


- (void)makeAlertPopupView
{
    CGRect frame = CGRectMake(0, 0, kAlertWidth, kAlertHeight);
    CGRect screen = [self mainScreenFrame];
    
    alertView = [[UIView alloc]initWithFrame:frame];
    alertView.center = CGPointMake(CGRectGetWidth(screen) / 2, CGRectGetHeight(screen) / 2);
    
    alertView.layer.masksToBounds = YES;
    alertView.backgroundColor = [UIColor colorWithWhite:1.0f alpha:1.0f];
    
    [self addSubview:alertView];
}


#pragma mark - View Animation Methods
- (void)moveAlertPopupView
{
    CGRect screen = [self mainScreenFrame];
    
    CATransform3D move = CATransform3DIdentity;
    CGFloat initAlertViewYPosition = (CGRectGetHeight(screen) + CGRectGetHeight(alertView.frame)) / 2;
    
    move = CATransform3DMakeTranslation(0, -initAlertViewYPosition, 0);
    move = CATransform3DRotate(move, 40 * M_PI / 180, 0, 0, 1.0f);
    
    alertView.layer.transform = move;
}

//弹出动画,带弹跳效果
- (void)showAlertView4Animate {
    
    [UIView animateWithDuration:1.0 delay:0.0 usingSpringWithDamping:0.4f initialSpringVelocity:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
        
        CATransform3D move = CATransform3DIdentity;
        alertView.layer.transform = move;
        
    } completion:^(BOOL finished) {
        
        
    }];
}


- (void)removeFromSuperview {
    
    [self.backImageView removeFromSuperview];
    self.backImageView = nil;
    
    [UIView animateWithDuration:0.6 delay:0.0 usingSpringWithDamping:1.0 initialSpringVelocity:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        
        CATransform3D move = CATransform3DIdentity;
        CGRect screen = [self mainScreenFrame];
        
        CGFloat initAlertViewYPosition = (CGRectGetHeight(screen) + CGRectGetHeight(self.frame)) / 2;
        
        move = CATransform3DMakeTranslation(0, initAlertViewYPosition, 0);
        move = CATransform3DRotate(move, 40 * M_PI / 180, 0, 0, 1.0f);
        
        alertView.layer.transform = move;
        self.backImageView.alpha = 0.0;
        
    } completion:^(BOOL finished) {
        
        [super removeFromSuperview];
    }];
}


- (CGRect)mainScreenFrame {
    
    return [UIScreen mainScreen].bounds;
}


- (void)clickLeftButton {
    
    [self removeFromSuperview];
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickLeftButtonDelegate)]) {
        
        [self.delegate clickLeftButtonDelegate];
    }
}


- (void)clickRightButton {
    
    [self removeFromSuperview];
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickRightButtonDelegate)]) {
        
        [self.delegate clickRightButtonDelegate];
    }
}


- (void)setShadowStyle:(UIButton *)currentButton {
    
    currentButton.layer.shadowOffset = CGSizeMake(1, 1);
    currentButton.layer.shadowOpacity = 0.6f;
    currentButton.layer.shadowRadius = 0.5;
}
@end


你可能感兴趣的:(自定义AlertView)