【iOS UI】自定義XAlertView、HUD、UIAlertController

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 設(shè)值方法
-(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 按鈕點擊方法 代理執(zhí)行代理方法
- (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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在日常iOS開發(fā)中,系統(tǒng)提供的控件常常無法滿足業(yè)務(wù)功能,這個時候需要我們實現(xiàn)一些自定義控件。自定義控件能讓我們完全...
    瀟瀟瀟瀟瀟瀟瀟閱讀 4,675評論 16 66
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,695評論 4 61
  • 幸福和你有關(guān) 那些甜言蜜語交織已不再是謊言 偶爾的打情罵俏會顯得更加浪漫 道一聲問候、說一聲晚安 幸福和...
    曹新慶閱讀 305評論 0 1
  • 文/程程 關(guān)注我,讓我成為你有趣靈魂的擺渡人。 2016年:關(guān)注一個偶像從34萬粉絲到42萬粉絲;加入一個讀書會從...
    程程的情懷閱讀 417評論 2 1
  • 蘇格拉底對于大家應(yīng)該都不陌生。但是你卻找不到一本他自己寫的書,甚至沒有他寫的一頁紙。我們都是從他的學(xué)生柏拉圖和色諾...
    尹二尹閱讀 1,026評論 0 0

友情鏈接更多精彩內(nèi)容