关注我博客的同学,又有福利了!!
今天分享的是 自定义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];
我还是太懒,懒得写注释。不过代码很简单好用,具体项目具体修改就行。想感慨一番来着,后来想想算了。
希望小伙伴们多写一些框架级可以复用的代码,让编程变得简单。加油。