模仿系统方法自定义UIAlertView的实现和代理

背景是博主偷懒依然用了iOS8后被废弃掉的UIAlertView,然后取消的字体比确定要粗,同时为了统一界面风格,需要把UIAlertView统一换成定制的样子,博主就犯难了,又不想大批量更改代码,怎么办,突然意识到为什么我不能自己写一个类似系统的方法呢,只要整体代码一样,不就可以通过搜索统一修改一下类名就好了么?说干就干,这里贴上代码:

#import <UIKit/UIKit.h>
@class MyAlertView;

@protocol MyAlertDelegate <NSObject>

-(void)alertView:(MyAlertView *)alertView clickedButtonAtIndex:(NSUInteger)buttonIndex;

@end

@interface MyAlertView : UIView

@property (weak,nonatomic) id<MyAlertDelegate>delegate;

-(instancetype)initWithTitle:title message:messge delegate:delegate cancelButtonTitle:cancelTitle otherButtonTitle:otherTitle;

-(void)show;

@end
#import "MyAlertView.h"

#define WIDTH [UIApplication sharedApplication].keyWindow.frame.size.width
#define HEIGHT [UIApplication sharedApplication].keyWindow.frame.size.height

@interface MyAlertView()
@property(nonatomic,strong)UIView *blackView;
@property(nonatomic,strong)UIView *alertBackView;
@property(nonatomic,strong)UILabel *contentLabel;
@property(nonatomic,strong)UIButton *cancelBtn;
@property(nonatomic,strong)UIButton *okBtn;
@property(nonnull,strong)NSString *title;
@property(nonnull,strong)NSString *content;
@property(nonnull,strong)NSString *cancelBtnTitle;
@property(nonnull,strong)NSString *okBtnTitle;
@end


@implementation MyAlertView


-(instancetype)initWithTitle:title message:messge delegate:delegate cancelButtonTitle:cancelTitle otherButtonTitle:otherTitle
{
    if (self=[super initWithFrame:[[UIApplication sharedApplication] keyWindow].frame]) {
        _delegate=delegate;
        _title=title;
        _content=messge;
        _cancelBtnTitle=cancelTitle;
        _okBtnTitle=otherTitle;
        [self setUp];
    }
    return self;
}
-(void)setUp
{
    NSLog(@"%f",WIDTH);
    NSLog(@"%f",HEIGHT);
    //黑色背景
    _blackView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    _blackView.backgroundColor=[UIColor blackColor];
    _blackView.alpha=0.6;
    [self addSubview:_blackView];
    //弹出框背景
    _alertBackView=[[UIView alloc]initWithFrame:CGRectMake(20, 44, WIDTH-40, 160)];
    _alertBackView.layer.cornerRadius=6;
    _alertBackView.center=CGPointMake(320/2, 568/2);
    _alertBackView.backgroundColor=[UIColor whiteColor];
    [self addSubview:_alertBackView];
    //标题
    UILabel *alertTitle=[[UILabel alloc]initWithFrame:CGRectMake(20, 10, WIDTH-40-40, 20)];
    alertTitle.text=_title;
    alertTitle.textAlignment=NSTextAlignmentCenter;
    alertTitle.textColor=[UIColor colorWithRed:0.31f green:0.31f blue:0.31f alpha:1.00f];
    alertTitle.font=[UIFont systemFontOfSize:15];
    [_alertBackView addSubview:alertTitle];
    //消息内容
    UILabel *message=[[UILabel alloc]initWithFrame:CGRectMake(20, 30, WIDTH-40-40, 60)];
    message.text=_content;
    message.numberOfLines=0;
    message.textAlignment=NSTextAlignmentCenter;
    message.textColor=[UIColor colorWithRed:0.31f green:0.31f blue:0.31f alpha:1.00f];
    message.font=[UIFont systemFontOfSize:15];
    [_alertBackView addSubview:message];

    //    CAKeyframeAnimation * animation;
    //    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    //    animation.duration = 0.5;
    //    animation.delegate = self;
    //    animation.removedOnCompletion = YES;
    //    animation.fillMode = kCAFillModeForwards;
    //
    //    NSMutableArray *values = [NSMutableArray array];
    //    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    //    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1.0)]];
    //    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 0.9)]];
    //    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    //
    //    animation.values = values;
    //    [_alertBackView.layer addAnimation:animation forKey:nil];

    //按钮背景
    UIView *btnBGView=[[UIView alloc]initWithFrame:CGRectMake(0, 100, WIDTH-40, 60)];
    btnBGView.backgroundColor=[UIColor colorWithRed:0.96f green:0.96f blue:0.96f alpha:1.00f];
    [_alertBackView addSubview:btnBGView];

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:btnBGView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(3, 3)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = btnBGView.bounds;
    maskLayer.path = maskPath.CGPath;
    btnBGView.layer.mask = maskLayer;
    if (_cancelBtnTitle==nil||_okBtnTitle==nil) {
        //确定按钮
        _okBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        _okBtn.frame=CGRectMake((WIDTH-40-220)/2, 15, 220, 30);
        _okBtn.titleLabel.font=[UIFont systemFontOfSize:15];
        [_okBtn setTitle:_cancelBtnTitle==nil ? _okBtnTitle:_cancelBtnTitle forState:UIControlStateNormal];
        [_okBtn setBackgroundImage:[UIImage imageNamed:@"login_loginbtn.png"] forState:UIControlStateNormal];
        [_okBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _okBtn.tag=1;
        [_okBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [btnBGView addSubview:_okBtn];
    }
    else
    {
        //取消按钮
        _cancelBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        _cancelBtn.frame=CGRectMake(25, 15,100, 30);
        _cancelBtn.titleLabel.font=[UIFont systemFontOfSize:15];
        [_cancelBtn setTitle:_cancelBtnTitle forState:UIControlStateNormal];
        [_cancelBtn setBackgroundImage:[UIImage imageNamed:@"detail_alert_left"] forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:[UIColor colorWithRed:0.47f green:0.47f blue:0.47f alpha:1.00f] forState:UIControlStateNormal];
        _cancelBtn.tag=0;
        [_cancelBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [btnBGView addSubview:_cancelBtn];
        //确定按钮
        _okBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        _okBtn.frame=CGRectMake(WIDTH-40-25-100, 15, 100, 30);
        _okBtn.titleLabel.font=[UIFont systemFontOfSize:15];
        [_okBtn setTitle:_okBtnTitle forState:UIControlStateNormal];
        [_okBtn setBackgroundImage:[UIImage imageNamed:@"detail_alert_right"] forState:UIControlStateNormal];
        [_okBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _okBtn.tag=1;
        [_okBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [btnBGView addSubview:_okBtn];

    }

}
-(void)buttonAction:(UIButton *)btn
{
    if ([self.delegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)]) {
        [self.delegate alertView:self clickedButtonAtIndex:(btn.tag)];
    }
    self.alpha = 0.0;
    [self removeFromSuperview];
    _blackView=nil;
    _alertBackView=nil;

}

-(void)show
{
    UIView * keywindow = [[UIApplication sharedApplication] keyWindow];
    [keywindow addSubview:self];
}

原谅我没写很多注释,不过我觉得大家都能看得懂,我并没有写的很复杂。这里需要注意的问题是,怎么把自己的alert添加到控制器上来显示,添加是简单,可是像系统那样,不在调用时设置frame和利用控制器addSubView怎么实现呢?博主一开始纠结了好久,不过最后算是写出来了,参考的小伙伴要认真看下,还有代理的实现步骤,需要格外注意下哦,代码中注释部分为动画效果,自定义alert弹出时不会像系统的那样直接让当前键盘收回,如果有键盘时触发,键盘有可能会遮挡到当前alert,需要在触发时手动对键盘resignFirstResponder。其他的博主就不详细说明了,代码中有体现。附上github下载地址:https://github.com/codeliu6572/CustomAlertView

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodingFire

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值