自定义AlertView

关注我博客的同学,又有福利了!!

今天分享的是 自定义AlertView。

这个在项目中很常用到。网上的代码也不少,但感觉都太麻烦了。来看看我这个吧!

DHAlertView.h


#import <UIKit/UIKit.h>
typedef enum {
    AlertType1 = 1,
    AlertType2,
    
} AlertType;

typedef void (^AlertBlock) (AlertType alertType);

@interface DHAlertView : UIView
{
    UIControl * _overlayView;
}

@property (nonatomic, copy) AlertBlock alertType;

- (void)show;
- (void)dismiss;

DHAlertView.m

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 6.0f;
        
        _overlayView = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        [_overlayView setAlpha:.0];
        [_overlayView addTarget:self action:@selector(touchBackAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self buildLayout];
    }
    return self;
}

- (void)buildLayout
{
    UIButton * oneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    oneBtn.frame = CGRectMake(35, 50, 100, 20);
    [oneBtn addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
    [oneBtn setTitle:@"按钮1" forState:UIControlStateNormal];
    [self addSubview:oneBtn];
    
    UIButton * twoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    twoBtn.frame = CGRectMake(150, 50, 100, 20);
    [twoBtn addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
    [twoBtn setTitle:@"按钮2" forState:UIControlStateNormal];
    [self addSubview:twoBtn];
    
    oneBtn.tag = 1;
    twoBtn.tag = 2;
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(35, 145, 220, 30);
    [cancelBtn addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    cancelBtn.titleLabel.font=[UIFont fontWithName:@"Arial" size:13];
  
    [self addSubview:cancelBtn];

}

#pragma mark -
#pragma mark event handlers

-(void)selectAction:(UIButton*)button{
    if (button.tag == 1) {
        _alertType(AlertType1);
    } else if(button.tag == 2){
        _alertType(AlertType2);
    }
}

- (void)cancelAction:(UIButton *)button
{
    [self dismiss];
}

- (void)touchBackAction:(UIControl *)control
{
    [self dismiss];
}

- (void)show
{
    UIWindow * keywindow = [[UIApplication sharedApplication] keyWindow];
    [keywindow addSubview:_overlayView];
    [keywindow addSubview:self];
    
    self.center = _overlayView.center;
    [self fadeIn];
    
}

- (void)dismiss
{
    [self fadeOut];
}

- (void)fadeIn
{
    self.transform = CGAffineTransformMakeScale(.3, .3);
    self.alpha = 0;
    
    [UIView animateWithDuration:.15 animations:^{
        [_overlayView setAlpha:.5];
        self.alpha = 1;
        self.transform = CGAffineTransformMakeScale(1, 1);
    }];
}
封装好了这个就可以使用了,举个栗子:

 DHAlertView * alertView = [[DHAlertView alloc] initWithFrame:CGRectMake(0, 0, 290, 200)];
 alertView.alertType = ^(AlertType alertType){
 if (alertType ==  AlertType1) {
 NSLog(@"AlertType1");
 } else if (alertType ==  AlertType2){
 NSLog(@"AlertType2");
 }
 };
 [alertView show];

我还是太懒,懒得写注释。不过代码很简单好用,具体项目具体修改就行。想感慨一番来着,后来想想算了。

希望小伙伴们多写一些框架级可以复用的代码,让编程变得简单。加油。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值