123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- //
- // 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 <FKAlertViewDelegate> 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 <FKAlertViewDelegate>)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<FKAlertViewDelegate>)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
|