单军华
2018-07-11 acdf41fa3b32b628d9d7bba1f975060567dad3d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//
//  RAlertView.m
//  RAlert
//
//  Created by roycms on 2016/10/11.
//  Copyright © 2016年 roycms. All rights reserved.
//
 
#import "RAlertView.h"
 
@interface RAlertView ()
 
@property (nonatomic,strong)NSArray *themeArray;
 
@end
@implementation RAlertView
 
 
/**
 init
 
 @param style style description
 @return return value description
 */
- (instancetype)initWithStyle:(AlertStyle)style {
    self = [super init];
    if (self) {
        [self initWindow:style];
    }
    return self;
}
 
/**
 init
 
 @param style style description
 @param width width description
 @return return value description
 */
- (instancetype)initWithStyle:(AlertStyle)style width:(CGFloat)width{
    self = [super init];
    if (self) {
        [self initWindow:style];
        [self setAlertWidth:width];
    }
    return self;
}
 
/**
 准备默认数据
 */
-(void)prepareData{
    
    self.themeArray = @[RGB16(0X1abc9c),
                        RGB16(0X27ae60),
                        RGB16(0X2980b9),
                        RGB16(0X2c3e50),
                        RGB16(0Xf39c12),
                        RGB16(0Xc0392b),
                        RGB16(0X7f8c8d),
                        RGB16(0X8e44ad)];
    
    [self.confirmButton setBackgroundColor:self.themeArray[(arc4random() % 8)]];
    [self.confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
 
/**
 设置弹框的宽度
 
 @param width width 宽度值  范围 0-1  百分比
 */
-(void)setAlertWidth:(CGFloat)width{
    
    [self.mainView mas_updateConstraints:^(MASConstraintMaker *make) {
        if (width > 1) {
            make.width.offset(width);
        }
        else if(width > 0 && width <= 1){
            make.width.offset([UIScreen mainScreen].bounds.size.width * width);
        }
        else{
            make.width.offset([UIScreen mainScreen].bounds.size.width * 0.7);
        }
    }];
}
 
/**
 设置 主题的 set 方法
 
 @param theme theme description
 */
-(void)setTheme:(UIColor*)theme{
    
    [self.confirmButton setBackgroundColor:theme];
    [self.confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
 
 
/**
 单击 背景 是否关闭弹窗
 
 @param isClickBackgroundCloseWindow isClickBackgroundCloseWindow description
 */
-(void)setIsClickBackgroundCloseWindow:(BOOL)isClickBackgroundCloseWindow{
    if(isClickBackgroundCloseWindow){
        UITapGestureRecognizer*tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(exit)];
        [self addGestureRecognizer:tapGesture];
    }
}
 
-(void)initWindow:(AlertStyle)style{
    
    [self viewInitUI];
    
    switch (style) {
        case SimpleAlert:
            [self simpleAlertViewInitUI];
            break;
        case ConfirmAlert:
            [self confirmAlertViewInitUI];
            
            break;
        case CancelAndConfirmAlert:
            [self cancelAndConfirmAlertViewInitUI];
            break;
    }
    
    [self animateSenior];
    [self prepareData];
}
 
/**
 view 初始化
 */
-(void)viewInitUI{
    
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    
    [window addSubview:self];
    [self addSubview:self.mainView];
    [self setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.8]];
    [self.mainView insertSubview:self.closedButton atIndex:999];
    [self.mainView addSubview:self.headerTitleLabel];
    [self.mainView insertSubview:self.contentView atIndex:0];
    
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(window);
    }];
    self.mainView.translatesAutoresizingMaskIntoConstraints =NO;
    
    [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.mainView.superview);
        make.width.offset([UIScreen mainScreen].bounds.size.width * 0.7);
    }];
 
    [self.closedButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mainView);
        make.right.equalTo(self.mainView);
        make.width.height.offset(35);
    }];
    [self.headerTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mainView).offset(10);
        make.left.equalTo(self.mainView).offset(15);
        make.right.equalTo(self.mainView).offset(-30);
    }];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerTitleLabel.mas_bottom).offset(5);
        make.left.equalTo(self.headerTitleLabel);
        make.right.equalTo(self.mainView).offset(-15);
        make.bottom.equalTo(self.mainView).offset(-10);
    }];
}
 
/**
 无按钮  弹窗样式
 */
-(void)simpleAlertViewInitUI{
    
    [self.contentTextLabel setFont:[UIFont systemFontOfSize:15]];
    [self.contentView addSubview:self.contentTextLabel];
    [self.contentTextLabel sizeToFit];
    
    [self.contentTextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(10);
        make.left.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView).offset(-10);
    }];
}
 
/**
 只有确认按钮 的弹窗样式
 */
-(void)confirmAlertViewInitUI{
    
    [self.mainView addSubview:self.confirmButton];
    [self.contentView addSubview:self.contentTextLabel];
    
    [self.contentTextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(10);
        make.left.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
    }];
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentTextLabel.mas_bottom).offset(10);
        make.left.right.equalTo(self.mainView);
        make.bottom.equalTo(self.mainView);
        make.height.offset(40);
    }];
}
 
/**
 有取消 按钮 和 确认按钮的弹窗样式
 */
-(void)cancelAndConfirmAlertViewInitUI{
    [self.mainView addSubview:self.cancelButton];
    [self.mainView addSubview:self.confirmButton];
    [self.contentView addSubview:self.contentTextLabel];
    
    [self.contentTextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(10);
        make.left.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
    }];
    
    NSMutableArray *arrayM = @[].mutableCopy;
    [arrayM addObject:self.cancelButton];
    [arrayM addObject:self.confirmButton];
    
    [arrayM mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    [arrayM mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@40);
        make.bottom.equalTo(self.mainView);
        make.top.equalTo(self.contentTextLabel.mas_bottom).offset(10);
    }];
}
 
/**
 弹窗飞入 动画
 */
-(void)animate{
    
    [self setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];
    [UIView animateWithDuration:0.12 animations:^{
        [self setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.8]];
    }];
    
    self.mainView.transform = CGAffineTransformMakeTranslation(0, 600);
    [UIView animateWithDuration:0.12 animations:^{
        self.mainView.transform = CGAffineTransformMakeTranslation(0, 0);
    }];
}
 
/**
 弹窗飞入 渐变 动画
 */
-(void)animateSenior{
 
    self.mainView.transform = CGAffineTransformMakeTranslation(0, 600);
    [UIView animateWithDuration:0.2 delay:0 usingSpringWithDamping:0.35 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveLinear animations:^{
        self.mainView.transform = CGAffineTransformMakeTranslation(0, 0);
    } completion:^(BOOL finished) {
    }];
}
 
#pragma make 事件
 
/**
 销毁弹窗  方法
 */
-(void)exit{
    [self removeFromSuperview];
}
 
/**
 关闭按钮事件
 
 @param sender sender description
 */
-(void)closedButtonClick:(UIButton *)sender{
    [self exit];
}
 
/**
 确定按钮的事件
 
 @param sender sender description
 */
-(void)confirmButtonClick:(UIButton*)sender{
    
    if(self.confirm){
        self.confirm();
    }
    [self exit];
}
 
/**
 取消按钮的 事件
 
 @param sender sender description
 */
-(void)cancelButtonClick:(UIButton*)sender{
    if(self.cancel){
        self.cancel();
    }
    [self exit];
}
 
#pragma make 懒加载
 
-(UIView*)mainView{
    if (_mainView == nil) {
        _mainView = [[UIView alloc]init];
        [_mainView setBackgroundColor:[UIColor whiteColor]];
        [_mainView.layer setMasksToBounds:YES];
        [_mainView.layer setCornerRadius:4];
    }
    return _mainView;
}
-(UIButton *)confirmButton{
    if(_confirmButton == nil){
        _confirmButton = [[UIButton alloc]init];
        [_confirmButton setBackgroundColor:RGB16(0Xfddb43)];
        [_confirmButton setTitle:@"Ok" forState:UIControlStateNormal];
        [_confirmButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_confirmButton setTitleColor:RGB16(0X3d3d3d) forState:UIControlStateNormal];
        [_confirmButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [_confirmButton addTarget:self action:@selector(confirmButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmButton;
}
-(UIButton *)cancelButton{
    if(_cancelButton == nil){
        _cancelButton = [[UIButton alloc]init];
        [_cancelButton setBackgroundColor:RGB16(0XEBECED)];
        [_cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_cancelButton setTitleColor:RGB16(0X3d3d3d) forState:UIControlStateNormal];
        [_cancelButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [_cancelButton addTarget:self action:@selector(cancelButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelButton;
}
 
-(UIButton *)closedButton{
    if(_closedButton == nil){
        _closedButton = [[UIButton alloc]init];
        [_closedButton setImage:[UIImage imageNamed:@"closed.png"] forState:UIControlStateNormal];
        [_closedButton addTarget:self action:@selector(closedButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closedButton;
}
 
-(UILabel*)headerTitleLabel {
    if (_headerTitleLabel == nil) {
        _headerTitleLabel = [[UILabel alloc]init];
        [_headerTitleLabel setFont:[UIFont systemFontOfSize:15]];
        [_headerTitleLabel setTextAlignment:NSTextAlignmentCenter];
        [_headerTitleLabel setTextColor:RGB16(0X3d3d3d)];
    }
    return _headerTitleLabel;
}
 
-(UIView *)contentView{
    if(_contentView==nil){
        _contentView =[[UIView alloc]init];
        [_contentView setBackgroundColor:[UIColor whiteColor]];
    }
    return _contentView;
}
 
-(UILabel*)contentTextLabel {
    if (_contentTextLabel == nil) {
        _contentTextLabel = [[UILabel alloc]init];
        [_contentTextLabel setFont:[UIFont systemFontOfSize:13]];
        [_contentTextLabel setTextAlignment:NSTextAlignmentCenter];
        [_contentTextLabel setTextColor:RGB16(0X898989)];
        _contentTextLabel.numberOfLines = 0;
    }
    return _contentTextLabel;
}
 
@end