// // FKAlertView.m // FirstLink // // Created by jack on 16/3/14. // Copyright © 2016年 FirstLink. All rights reserved. // #import "FKAlertView.h" @interface FKAlertView () @property (nonatomic, strong) UIImage *image; @property (nonatomic, strong) NSString *title; @property (nonatomic, strong) NSString *message; @property (nonatomic, strong) NSString *cancelTitle; @property (nonatomic, strong) NSString *doneTitle; @property (nonatomic, strong) UIImageView *imageView; @property (nonatomic, strong) UIView *coverView; @property (nonatomic, strong) UIView *contentView; @property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) UILabel *messageLabel; @property (nonatomic, strong) UIButton *cancelBtn; @property (nonatomic, strong) UIButton *doneBtn; @property (nonatomic, weak) id delegate; @property (nonatomic, strong) NSDictionary *userInfo; @end @implementation FKAlertView - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]){ } return self; } - (instancetype)initWithMessage:(NSString *)message delegate:(id )delegate cancelButtonTitle:(NSString *)cancelTitle doneButtonTitle:(NSString *)doneTitle info:(NSDictionary *)info{ return [self initWithImage:nil title:nil message:message delegate:delegate cancelButtonTitle:cancelTitle doneButtonTitle:doneTitle info:info]; } - (instancetype)initWithImage:(UIImage *)image title:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelTitle doneButtonTitle:(NSString *)doneTitle info:(NSDictionary *)info{ if (self = [super init]) { self.image = image; self.title = title; self.message = message; self.cancelTitle = cancelTitle; self.doneTitle = doneTitle; self.delegate = delegate; self.userInfo = info; [self _initialize]; } return self; } - (void)_initialize{ UIView *vertLine = ({ UIView *view = [[UIView alloc]init]; view.backgroundColor = UIColorFromRGB(0xe5e5e5); view; }); UIView *horLine = ({ UIView *view = [[UIView alloc]init]; view.backgroundColor = UIColorFromRGB(0xe5e5e5); view; }); BOOL showTitleArea = YES; CGFloat contentH = 150.0f; if (!self.image || !self.title){ showTitleArea = NO; contentH = 120.0f; } [self addSubview:self.coverView]; [self addSubview:self.contentView]; [self.contentView addSubview:vertLine]; [self.contentView addSubview:horLine]; [self.contentView addSubview:self.messageLabel]; [self.contentView addSubview:self.cancelBtn]; [self.contentView addSubview:self.doneBtn]; if (showTitleArea){ [self.contentView addSubview:self.imageView]; [self.contentView addSubview:self.titleLabel]; } [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.insets(UIEdgeInsetsZero); }]; [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self).offset(150); make.centerX.equalTo(self); make.size.mas_equalTo(CGSizeMake(250, contentH)); }]; if (showTitleArea){ [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.contentView).offset(20); make.left.equalTo(self.contentView).offset(49); make.width.height.mas_equalTo(35); }]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.imageView.mas_right).offset(15); make.centerY.equalTo(self.imageView); }]; } [horLine mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.contentView); make.bottom.equalTo(self.contentView).offset(- 50); make.height.mas_equalTo(0.5); }]; [vertLine mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(horLine.mas_bottom); make.centerX.bottom.equalTo(self.contentView); make.width.mas_equalTo(0.5); }]; [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) { if (showTitleArea){ make.top.equalTo(self.imageView.mas_bottom); }else{ make.top.equalTo(self.contentView); } make.bottom.equalTo(horLine.mas_top); make.centerX.equalTo(self.contentView); }]; [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.bottom.equalTo(self.contentView); make.top.equalTo(horLine.mas_bottom); make.right.equalTo(vertLine.mas_left); }]; [self.doneBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.bottom.equalTo(self.contentView); make.left.equalTo(vertLine.mas_right); make.top.equalTo(horLine.mas_bottom); }]; } - (void)showInView:(UIView *)view{ if (!view) return; self.coverView.backgroundColor = [UIColor clearColor]; self.contentView.alpha = 0.0f; [view addSubview:self]; [self mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.insets(UIEdgeInsetsZero); }]; [self setNeedsLayout]; [self layoutIfNeeded]; WeakSelf(weakSelf); [UIView animateWithDuration:0.15f animations:^{ weakSelf.coverView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5]; weakSelf.contentView.alpha = 1.0f; } completion:nil]; } - (void)dismiss{ [self removeFromSuperview]; } #pragma mark - action - (void)clickDoneBtn:(UIButton *)sender{ if ([self.delegate respondsToSelector:@selector(fk_alertView:doneWithConfirm:info:)]){ [self.delegate fk_alertView:self doneWithConfirm:YES info:self.userInfo]; } [self dismiss]; } - (void)clickCancelBtn:(UIButton *)sender{ if ([self.delegate respondsToSelector:@selector(fk_alertView:doneWithConfirm:info:)]){ [self.delegate fk_alertView:self doneWithConfirm:NO info:self.userInfo]; } [self dismiss]; } #pragma mark - property - (UIView *)coverView{ if (_coverView == nil) { _coverView = [[UIView alloc]init]; _coverView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5]; } return _coverView; } - (UIView *)contentView { if (_contentView == nil) { _contentView = [[UIView alloc]init]; _contentView.backgroundColor = UIColorFromRGB(0xffffff); _contentView.layer.cornerRadius = 8; } return _contentView; } - (UIImageView *)imageView{ if (_imageView == nil) { _imageView = [[UIImageView alloc]initWithImage:self.image]; } return _imageView; } - (UILabel *)titleLabel{ if (_titleLabel == nil) { _titleLabel = [[UILabel alloc]init]; _titleLabel.font = [UIFont systemFontOfSize:27]; _titleLabel.textColor = UIColorFromRGB(0xff6362); _titleLabel.numberOfLines = 1; _titleLabel.text = self.title; } return _titleLabel; } - (UILabel *)messageLabel{ if (_messageLabel == nil) { _messageLabel = [[UILabel alloc]init]; _messageLabel.font = [UIFont systemFontOfSize:15]; _messageLabel.textColor = UIColorFromRGB(0x333333); _messageLabel.numberOfLines = 1; _messageLabel.textAlignment = NSTextAlignmentCenter; _messageLabel.text = self.message; } return _messageLabel; } - (UIButton *)cancelBtn{ if (_cancelBtn == nil) { _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:15]; [_cancelBtn setTitleColor:UIColorFromRGB(0x999999) forState:UIControlStateNormal]; [_cancelBtn addTarget:self action:@selector(clickCancelBtn:) forControlEvents:UIControlEventTouchUpInside]; [_cancelBtn setTitle:self.cancelTitle forState:UIControlStateNormal]; } return _cancelBtn; } - (UIButton *)doneBtn{ if (_doneBtn == nil) { _doneBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _doneBtn.titleLabel.font = [UIFont systemFontOfSize:15]; [_doneBtn setTitleColor:UIColorFromRGB(0xff624a) forState:UIControlStateNormal]; [_doneBtn addTarget:self action:@selector(clickDoneBtn:) forControlEvents:UIControlEventTouchUpInside]; [_doneBtn setTitle:self.doneTitle forState:UIControlStateNormal]; } return _doneBtn; } @end