-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMMCustomAlertView.m
232 lines (166 loc) · 8.4 KB
/
MMCustomAlertView.m
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
//
// MMCustomAlertView.m
//
// Created by Michalis Mavris on 04/10/14.
// Copyright (c) 2014 miksoft.net. All rights reserved.
//
#import "MMCustomAlertView.h"
@interface MMCustomAlertView()
@property (nonatomic, assign) id <MMCustomAlertViewDelegate> delegate;
@property (nonatomic, strong) UILabel *msgLabel;
@property (nonatomic, strong) UIButton *okButton;
@property (nonatomic, strong) UIImageView *backgroundImageView;
@end
@implementation MMCustomAlertView{
BOOL isAlertAnimated;
}
-(id)initWithMessage:(NSString*)msg andDelegate:(id<MMCustomAlertViewDelegate>)del{
self = [super initWithFrame:[[UIScreen mainScreen] bounds]];
if (self) {
self.delegate=del;
isAlertAnimated=YES;
[self addSubview:self.msgLabel];
[self addSubview:self.okButton];
self.alpha=0;
self.backgroundColor = [UIColor blackColor];
self.msgLabel.text=msg;
self.msgLabel.textColor=[UIColor whiteColor];
self.okButton.tintColor=[UIColor whiteColor];
[self setupConstraints];
}
//Animate the alpha of View
[UIView animateWithDuration:0.3 animations:^{self.alpha=0.9;} completion:^(BOOL finished){}];
return self;
}
-(id)initWithMessage:(NSString*)msg backgroundColor:(UIColor*)backgroundColor textColor:(UIColor*)textColor viewAlpha:(float)alpha animated:(BOOL)isAnimated andDelegate:(id<MMCustomAlertViewDelegate>)del{
self = [super initWithFrame:[[UIScreen mainScreen] bounds]];
if (self) {
self.delegate=del;
isAlertAnimated=isAnimated;
[self addSubview:self.msgLabel];
[self addSubview:self.okButton];
self.alpha=0;
self.backgroundColor = backgroundColor;
self.msgLabel.textColor=textColor;
self.msgLabel.text=msg;
self.okButton.tintColor =textColor;
[self setupConstraints];
}
//Check if the user want to animate the Alert View
if (isAlertAnimated) {
//Animate the alpha of View
[UIView animateWithDuration:0.3 animations:^{self.alpha=alpha;} completion:^(BOOL finished){}];
}
else{
self.alpha=alpha;
}
return self;
}
- (UILabel *)msgLabel {
if (_msgLabel == nil) {
_msgLabel = [[UILabel alloc] init];
[_msgLabel setNumberOfLines:0];
[_msgLabel sizeToFit];
[_msgLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[_msgLabel setBackgroundColor:[UIColor clearColor]];
[_msgLabel setTextAlignment:NSTextAlignmentCenter];
}
return _msgLabel;
}
- (UIButton *)okButton {
if (_okButton == nil) {
_okButton = [UIButton buttonWithType:UIButtonTypeSystem];
[_okButton setTranslatesAutoresizingMaskIntoConstraints:NO];
[_okButton setBackgroundColor:[UIColor clearColor]];
[_okButton setTitle:@"OK" forState:UIControlStateNormal];
[_okButton addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
}
return _okButton;
}
-(void)buttonClicked{
if (isAlertAnimated) {
//Animate, removing the alpha of View and call delegate
[UIView animateWithDuration:0.3
animations:^{
self.alpha=0;
}
completion:^(BOOL finished){
[self removeFromSuperview];
[self.delegate MMCustomAlertViewOKButtonDelegate];
}];
}
else{
//Removing the alpha of View and call delegate
[self removeFromSuperview];
[self.delegate MMCustomAlertViewOKButtonDelegate];
}
}
- (void)setupConstraints{
//msgLabel constraints
// Width constraint
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgLabel
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeWidth
multiplier:0.8
constant:0]];
// Height constraint
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgLabel
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeHeight
multiplier:0.2
constant:0]];
// Center horizontally
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgLabel
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
// Center vertically
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.msgLabel
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterY
multiplier:0.6
constant:0.0]];
//okButton constraints
// Width constraint
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.okButton
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeWidth
multiplier:0.8
constant:0]];
// Height constraint
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.okButton
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeHeight
multiplier:0.08
constant:0]];
// Center horizontally
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.okButton
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
// Center vertically
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.okButton
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
}
@end