美文网首页
【iOS UI】自定义XAlertView、HUD、UIAler

【iOS UI】自定义XAlertView、HUD、UIAler

作者: XIAO_WEN | 来源:发表于2017-05-18 14:36 被阅读76次
    XAlertView

    一、自定义XAlertView继承自UIView

    XAlertView.h
    
    #define kXAlertViewWidth (kScreenWidth*250/375)
    #define kXAlertViewHeight (kScreenHeight*150/667)
    //自定义XAlertView👆
    
    #import <UIKit/UIKit.h>
    
    //代理
    @protocol XAlertViewDelegate <NSObject>
    
    //代理方法
    - (void)yesBtnPressed;
    - (void)noBtnPressed;
    
    
    @end
    
    @interface XAlertView : UIView
    //属性
    @property (nonatomic, assign) NSString *title;
    @property (nonatomic, assign) NSString *message;
    @property (nonatomic, strong) UIWindow *alphaWindow;
    //代理
    @property (weak, nonatomic) id<XAlertViewDelegate> delegate;
    
    //方法
    - (void) showAnimated;
    
    @end
    
    XAlertView.m
    
    #import "XAlertView.h"
    
    @interface XAlertView ()
    //私有属性
    //@property (nonatomic, strong) UIWindow *alphaWindow;
    @property (nonatomic, strong) UILabel *titleLabel;
    @property (nonatomic, strong) UILabel *messageLabel;
    @property (nonatomic, strong) UIButton *yesBtn;
    @property (nonatomic, strong) UIButton *noBtn;
    
    @end
    
    @implementation XAlertView
    
    -(instancetype)initWithFrame:(CGRect)frame{
        if (self = [super initWithFrame:frame]) {
            //背景色
            self.backgroundColor = [UIColor colorWithRed:70/255.0 green:130/255.0 blue:180/255.0 alpha:0.8];
            //圆角
            self.layer.cornerRadius = 15;
            self.layer.masksToBounds = YES;
            
            //添加控件
            [self addSubview:self.titleLabel];
            [self addSubview:self.messageLabel];
            [self addSubview:self.noBtn];
            [self addSubview:self.yesBtn];
        }
        return self;
    }
    
    
    #pragma mark 懒加载控件
    -(UIWindow *)alphaWindow{
        if (!_alphaWindow) {
            _alphaWindow = [[UIWindow alloc] init];
            _alphaWindow.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
            _alphaWindow.windowLevel = 101;
        }
        return _alphaWindow;
    }
    
    -(UILabel *)titleLabel{
        if (!_titleLabel) {
            _titleLabel = [[UILabel alloc] init];
            _titleLabel.font = [UIFont fontWithName:@"Monaco" size:18];
            _titleLabel.textAlignment = NSTextAlignmentCenter;
            _titleLabel.textColor = [UIColor whiteColor];
        }
        return _titleLabel;
    }
    
    -(UILabel *)messageLabel{
        if (!_messageLabel) {
            _messageLabel = [[UILabel alloc] init];
            _messageLabel.font = [UIFont fontWithName:@"Monaco" size:12];
            _messageLabel.textAlignment = NSTextAlignmentCenter;
            _messageLabel.textColor = [UIColor whiteColor];
        }
        return _messageLabel;
    }
    
    -(UIButton *)noBtn{
        if (!_noBtn) {
            _noBtn = [[UIButton alloc] init];
            [_noBtn addTarget:self action:@selector(noBtnPressed) forControlEvents:UIControlEventTouchDown];
            [_noBtn setImage:[UIImage imageNamed:@"no"] forState:UIControlStateNormal];
            _noBtn.frame = CGRectMake(30, self.frame.size.height - 15 - 28, 28, 28);
        }
        return _noBtn;
    }
    
    -(UIButton *)yesBtn{
        if (!_yesBtn) {
            _yesBtn = [[UIButton alloc] init];
            [_yesBtn addTarget:self action:@selector(yesBtnPressed) forControlEvents:UIControlEventTouchDown];
            [_yesBtn setImage:[UIImage imageNamed:@"yes"] forState:UIControlStateNormal];
            _yesBtn.frame = CGRectMake(self.frame.size.width - 30 - 28, self.noBtn.frame.origin.y
                                       - 5, 35, 35);
        }
        return _yesBtn;
    }
    
    #pragma mark 设值方法
    -(void)setTitle:(NSString *)title{
        if (title) {
            _title = title;
            self.titleLabel.text = title;
            [self.titleLabel sizeToFit];
            CGRect rect = self.titleLabel.frame;
            rect.origin.x = self.frame.size.width/2 - rect.size.width/2;
            rect.origin.y = 10;
            self.titleLabel.frame = rect;
        }
    }
    
    -(void)setMessage:(NSString *)message{
        if (message) {
            _message = message;
            self.messageLabel.text = message;
            [self.messageLabel sizeToFit];
            CGRect rect = self.messageLabel.frame;
            rect.origin.x = self.frame.size.width/2 - rect.size.width/2;
            rect.origin.y = CGRectGetMaxY(self.titleLabel.frame) + 10;
            self.messageLabel.frame = rect;
        }
    }
    
    #pragma mark 带动画显示
    -(void)showAnimated{
        self.alphaWindow.frame = [UIScreen mainScreen].bounds;
        [self.alphaWindow makeKeyAndVisible];
        [self.alphaWindow addSubview:self];
        
        //动画效果👇
        CAKeyframeAnimation *animation;
        animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
        animation.duration = 0.25;
        animation.removedOnCompletion = YES;
        animation.fillMode = kCAFillModeForwards;
        
        NSMutableArray *values = [NSMutableArray array];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)]];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1.1)]];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
        
        animation.values = values;
        
        [self.layer addAnimation:animation forKey:nil];
        //动画效果👆
    }
    
    #pragma mark 按钮点击方法 代理执行代理方法
    - (void)noBtnPressed{
        if ([self.delegate respondsToSelector:@selector(noBtnPressed)]) {
            [self removeFromSuperview];
            [self.delegate noBtnPressed];
        }
    }
    
    - (void)yesBtnPressed{
        if ([_delegate respondsToSelector:@selector(yesBtnPressed)]) {
            [self removeFromSuperview];
            [_delegate yesBtnPressed];
        }
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:【iOS UI】自定义XAlertView、HUD、UIAler

          本文链接:https://www.haomeiwen.com/subject/doysxxtx.html