首页 > 代码库 > 自定义UIAlertView

自定义UIAlertView

            code4App上面有很多很棒的UI特效代码,我们常感叹,好牛逼啊,这效果,太炫了,哇,怎么自己写不出来.其实,再炫的特效,都是根据苹果系统的框架而来,如果我们了解系统框架实现的原理,也就能写出属于自己自定义的控件,加上各种各样的动画.

           这里,我就展示一个自定义的UIAlertView效果控件,视图出现的时候动画-先放大-再缩小-最后成正常比例,消失的时候缩小加渐隐.调用也很方便,不需要像系统先创建后[alert show],我在类内部就已经写好了,只需要alloc创建,调用各个按钮对应的响应block就行.

           @.h

#import <UIKit/UIKit.h>

typedef void(^Myblcok)();

@interface CustomAlertView : UIView

- (id)initWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelTitle firstButtonTitles:(NSString *)firstTitle senondButtonTitles:(NSString *)secondTitle thirdButtonTitles:(NSString *)thirdTitle;

//  利用block将按钮的点击事件传出去
@property (nonatomic,copy)Myblcok cancelBlock;
@property (nonatomic,copy)Myblcok firstBlcok;
@property (nonatomic,copy)Myblcok secondBlock;
@property (nonatomic,copy)Myblcok thirdBlock;

@end


@interface UIImage (colorful)

+ (UIImage *)imageWithColor:(UIColor *)color;

@end
          @.m

//
//  CustomAlertView.m
//  CustomAlertView
//
//  Created by 胡明涛 on 14-5-6.
//  Copyright (c) 2014年 胡明涛. All rights reserved.
//

#import "CustomAlertView.h"

// 屏幕的物理高度
#define  ScreenHeight  [UIScreen mainScreen].bounds.size.height

// 屏幕的物理宽度
#define  ScreenWidth   [UIScreen mainScreen].bounds.size.width
#define AlertViewHeight   260
#define AlertViewWidth    200

@interface CustomAlertView ()

@property (nonatomic,strong) UIView   *backgroundView;   //  底部View,阻挡其他事件响应
@property (nonatomic,strong) UILabel  *titleLabel;       //  标题
@property (nonatomic,strong) UIButton *cancelButton;     //  取消


@end

@implementation CustomAlertView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


- (id)initWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelTitle firstButtonTitles:(NSString *)firstTitle senondButtonTitles:(NSString *)secondTitle thirdButtonTitles:(NSString *)thirdTitle{

    self = [super initWithFrame:CGRectMake((ScreenWidth - AlertViewWidth)/2.0, (ScreenHeight-AlertViewHeight)/2 ,AlertViewWidth, AlertViewHeight)];
    if (self) {
        
        [self createCustomAlertView];
        self.titleLabel.text = title;
        [self.cancelButton setTitle:cancelTitle forState:UIControlStateNormal];

        if (thirdTitle != nil) {
            
            [((UIButton *)[self viewWithTag:200]) setTitle:firstTitle forState:UIControlStateNormal];
            [((UIButton *)[self viewWithTag:201]) setTitle:secondTitle forState:UIControlStateNormal];
            [((UIButton *)[self viewWithTag:202]) setTitle:thirdTitle forState:UIControlStateNormal];
        
        }else{
        
            [((UIButton *)[self viewWithTag:200]) setTitle:firstTitle forState:UIControlStateNormal];
            ((UIButton *)[self viewWithTag:200]).frame = CGRectMake(10, 60, self.bounds.size.width-20, 40);
            [((UIButton *)[self viewWithTag:201]) setTitle:secondTitle forState:UIControlStateNormal];
            ((UIButton *)[self viewWithTag:201]).frame = CGRectMake(10, 130, self.bounds.size.width-20, 40);
            [((UIButton *)[self viewWithTag:202]) removeFromSuperview];
        
        }
        
        __block CustomAlertView * SELF = self;
        [UIView animateWithDuration:0.2 animations:^{
           
            SELF.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.5, 0.5);

        }completion:^(BOOL finished) {
            
            [UIView animateWithDuration:0.2 animations:^{
                
                SELF.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.3, 1.3);
                
            } completion:^(BOOL finished) {
               
                [UIView animateWithDuration:0.2 animations:^{
                    
                    SELF.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);
                    
                } completion:^(BOOL finished) {
                    
                }];
            }];
            
        }];
        
    }
    
    // 阻碍其他响应事件
    self.backgroundView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    _backgroundView.backgroundColor = [UIColor blackColor];
    _backgroundView.alpha = 0.3;
    [[UIApplication sharedApplication].keyWindow addSubview:_backgroundView];

    [[UIApplication sharedApplication].keyWindow addSubview:self];

    return  self;
}

- (void)createCustomAlertView{
    
    
    self.backgroundColor = [UIColor whiteColor];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, self.bounds.size.width, 40)];
    _titleLabel.textColor = [UIColor redColor];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_titleLabel];
   
    //  取消按钮
    self.cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _cancelButton.frame = CGRectMake(10,AlertViewHeight - 50,self.bounds.size.width-20,40);
    _cancelButton.tag = 100;
    [_cancelButton setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:227.0/255.0 green:100.0/255.0 blue:83.0/255.0 alpha:1]] forState:UIControlStateNormal];
    [_cancelButton addTarget:self action:@selector(didClickButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_cancelButton];
    
    for (int i = 0; i < 3; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(10, 20 +i*60, self.bounds.size.width-20, 40);
        button.tag = 200 + i;
        [button setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:87.0/255.0 green:135.0/255.0 blue:173.0/255.0 alpha:1]] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(didClickButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
    
    
    
}

- (void)didClickButtonAction:(UIButton *)button{

    switch (button.tag) {
        case 100:
            if (_cancelBlock) {
                
                _cancelBlock();
               [self dismissAlertView];
            }
            break;
        case 200:
            
            if (_firstBlcok) {
                
                _firstBlcok();
                [self dismissAlertView];
            }
            break;
        case 201:
            
            if (_secondBlock) {
                
                _secondBlock();
                [self dismissAlertView];
            }
            break;
        case 202:
            
            if (_thirdBlock) {
                
                _thirdBlock();
                [self dismissAlertView];
            }
            break;
        default:
            break;
    }

}

//  取消视图
- (void)dismissAlertView{

    [UIView animateWithDuration:1.0 animations:^{
        
        self.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.0, 0.0);
        self.alpha = 0.0;
        self.backgroundView.alpha = 0.0;
    }completion:^(BOOL finished) {
        
        self.cancelBlock = nil;
        self.firstBlcok = nil;
        self.secondBlock = nil;
        self.thirdBlock = nil;
        [_backgroundView removeFromSuperview];
        [self removeFromSuperview];
        _backgroundView = nil;

    }];

}

@end


@implementation UIImage (colorful)

+ (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

@end
 
           @调用

- (void)didClickButtonAction{
    
    CustomAlertView *alertView = [[CustomAlertView alloc] initWithTitle:@"HMT" cancelButtonTitle:@"取消" firstButtonTitles:@"first" senondButtonTitles:@"second" thirdButtonTitles:@"third"];
    [alertView setCancelBlock:^(){
        
        NSLog(@"点击了取消按钮");
    }];
    [alertView setFirstBlcok:^(){
        
        NSLog(@"点击了first按钮");
    }];
    //............以下类似
}
           @效果:(是不是觉得有的时候也能代替UIActionSheet)