Skip to content

Commit 4b56da8

Browse files
12207480tanyang
authored andcommitted
add alert handle
1 parent 5bf6ec8 commit 4b56da8

File tree

7 files changed

+68
-57
lines changed

7 files changed

+68
-57
lines changed

TYAlertControllerDemo/TYAlertController/TYAlertController.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ typedef NS_ENUM(NSInteger, TYAlertTransitionAnimation) {
2424

2525
@property (nonatomic, strong, readonly) UIView *alertView;
2626

27+
@property (nonatomic, strong) UIView *backgroundView; // set .backgroundColor,or you set coustom view to it
28+
2729
@property (nonatomic, assign, readonly) TYAlertControllerStyle preferredStyle;
2830

2931
@property (nonatomic, assign, readonly) TYAlertTransitionAnimation transitionAnimation;

TYAlertControllerDemo/TYAlertController/TYAlertController.m

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
#import "TYAlertController.h"
10+
#import "UIView+TYAutoLayout.h"
1011

1112
@interface TYAlertController ()
1213

@@ -79,16 +80,30 @@ + (instancetype)alertControllerWithAlertView:(UIView *)alertView preferredStyle:
7980
- (void)viewDidLoad {
8081
[super viewDidLoad];
8182
// Do any additional setup after loading the view.
82-
self.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
83+
self.view.backgroundColor = [UIColor clearColor];
84+
[self addBackgroundView];
8385

8486
[self addSingleTapGesture];
8587

8688
[self configureAlertView];
89+
90+
[self.view layoutIfNeeded];
91+
92+
}
93+
94+
- (UIView *)backgroundView
95+
{
96+
if (_backgroundView == nil) {
97+
_backgroundView = [[UIView alloc]init];
98+
_backgroundView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
99+
}
100+
return _backgroundView;
87101
}
88102

89-
- (void)viewWillAppear:(BOOL)animated
103+
- (void)addBackgroundView
90104
{
91-
[super viewWillAppear:animated];
105+
self.backgroundView.frame = self.view.bounds;
106+
[self.view addSubview:self.backgroundView];
92107
}
93108

94109
- (void)addSingleTapGesture
@@ -105,7 +120,7 @@ - (void)addSingleTapGesture
105120
//点击次数
106121
singleTap.numberOfTapsRequired = 1;
107122
//增加事件者响应者,
108-
[self.view addGestureRecognizer:singleTap];
123+
[self.backgroundView addGestureRecognizer:singleTap];
109124
}
110125

111126
- (void)configureController
@@ -144,21 +159,20 @@ - (void)configureAlertView
144159
- (void)layoutAlertStyleView
145160
{
146161
// center X
147-
NSLayoutConstraint *alertViewCenterXConstraint = [NSLayoutConstraint constraintWithItem:_alertView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
162+
[self.view addConstraintCenterXToView:_alertView CenterYToView:nil];
148163

149164
// top Y
150-
NSLayoutConstraint *alertViewTopYConstraint = nil;
151165
if (_alertViewOriginY > 0) {
152-
alertViewTopYConstraint = [NSLayoutConstraint constraintWithItem:_alertView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:_alertViewOriginY];
166+
167+
[self.view addConstarintWithView:_alertView topView:self.view leftView:nil bottomView:nil rightView:nil edageInset:UIEdgeInsetsMake(_alertViewOriginY, 0, 0, 0)];
153168
}else {
154-
alertViewTopYConstraint = [NSLayoutConstraint constraintWithItem:_alertView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0];
169+
[self.view addConstraintCenterXToView:nil CenterYToView:_alertView];
155170
}
156171

157172
if (!CGSizeEqualToSize(_alertView.frame.size,CGSizeZero)) {
158-
// height
159-
[_alertView addConstraint:[NSLayoutConstraint constraintWithItem:_alertView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:CGRectGetHeight(_alertView.frame)]];
160173
// width
161-
[_alertView addConstraint:[NSLayoutConstraint constraintWithItem:_alertView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:CGRectGetWidth(_alertView.frame)]];
174+
[_alertView addConstarintWidth:CGRectGetWidth(_alertView.frame) height:CGRectGetHeight(_alertView.frame)];
175+
162176
}else {
163177
BOOL findAlertViewWidthConstraint = NO;
164178
for (NSLayoutConstraint *constraint in _alertView.constraints) {
@@ -169,31 +183,23 @@ - (void)layoutAlertStyleView
169183
}
170184

171185
if (!findAlertViewWidthConstraint) {
172-
// width
173-
[_alertView addConstraint:[NSLayoutConstraint constraintWithItem:_alertView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:CGRectGetWidth(self.view.frame)-2*_alertViewEdging]];
186+
[_alertView addConstarintWidth:CGRectGetWidth(self.view.frame)-2*_alertViewEdging height:0];
174187
}
175188
}
176-
177-
[self.view addConstraints:@[alertViewCenterXConstraint,alertViewTopYConstraint]];
178189
}
179190

180191
- (void)layoutActionSheetStyleView
181192
{
182193
// center X
183-
NSLayoutConstraint *alertViewCenterXConstraint = [NSLayoutConstraint constraintWithItem:_alertView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
184-
// Bottom
185-
NSLayoutConstraint *alertViewButtomYConstraint = [NSLayoutConstraint constraintWithItem:_alertView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
186-
// left
187-
NSLayoutConstraint *alertViewLeftConstraint = [NSLayoutConstraint constraintWithItem:_alertView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
188-
// right
189-
NSLayoutConstraint *alertViewRightConstraint = [NSLayoutConstraint constraintWithItem:_alertView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
194+
[self.view addConstraintCenterXToView:_alertView CenterYToView:nil];
195+
196+
[self.view addConstarintWithView:_alertView topView:nil leftView:self.view bottomView:self.view rightView:self.view edageInset:UIEdgeInsetsZero];
190197

191198
if (CGRectGetHeight(_alertView.frame) > 0) {
192199
// height
193-
[_alertView addConstraint:[NSLayoutConstraint constraintWithItem:_alertView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1 constant:CGRectGetHeight(_alertView.frame)]];
200+
[_alertView addConstarintWidth:0 height:CGRectGetHeight(_alertView.frame)];
194201
}
195-
196-
[self.view addConstraints:@[alertViewCenterXConstraint,alertViewButtomYConstraint,alertViewLeftConstraint,alertViewRightConstraint]];
202+
197203
}
198204

199205
#pragma mark - action
@@ -208,4 +214,9 @@ - (void)didReceiveMemoryWarning {
208214
// Dispose of any resources that can be recreated.
209215
}
210216

217+
- (void)dealloc
218+
{
219+
NSLog(@"TYAlertController dealloc");
220+
}
221+
211222
@end

TYAlertControllerDemo/TYAlertController/TYAlertFadeAnimation.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ - (void)presentAnimateTransition:(id<UIViewControllerContextTransitioning>)trans
2222
{
2323
TYAlertController *alertController = (TYAlertController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
2424

25-
alertController.view.alpha = 0.0;
25+
alertController.backgroundView.alpha = 0.0;
2626

2727
switch (alertController.preferredStyle) {
2828
case TYAlertControllerStyleAlert:
@@ -41,7 +41,7 @@ - (void)presentAnimateTransition:(id<UIViewControllerContextTransitioning>)trans
4141
[containerView addSubview:alertController.view];
4242

4343
[UIView animateWithDuration:0.25 animations:^{
44-
alertController.view.alpha = 1.0;
44+
alertController.backgroundView.alpha = 1.0;
4545
switch (alertController.preferredStyle) {
4646
case TYAlertControllerStyleAlert:
4747
alertController.alertView.alpha = 1.0;
@@ -68,7 +68,7 @@ - (void)dismissAnimateTransition:(id<UIViewControllerContextTransitioning>)trans
6868
TYAlertController *alertController = (TYAlertController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
6969

7070
[UIView animateWithDuration:0.25 animations:^{
71-
alertController.view.alpha = 0.0;
71+
alertController.backgroundView.alpha = 0.0;
7272
switch (alertController.preferredStyle) {
7373
case TYAlertControllerStyleAlert:
7474
alertController.alertView.alpha = 0.0;

TYAlertControllerDemo/TYAlertController/TYAlertScaleFadeAnimation.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ - (void)presentAnimateTransition:(id<UIViewControllerContextTransitioning>)trans
1919
{
2020
TYAlertController *alertController = (TYAlertController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
2121

22-
alertController.view.alpha = 0.0;
22+
alertController.backgroundView.alpha = 0.0;
2323

2424
switch (alertController.preferredStyle) {
2525
case TYAlertControllerStyleAlert:
@@ -38,7 +38,7 @@ - (void)presentAnimateTransition:(id<UIViewControllerContextTransitioning>)trans
3838
[containerView addSubview:alertController.view];
3939

4040
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
41-
alertController.view.alpha = 1.0;
41+
alertController.backgroundView.alpha = 1.0;
4242
switch (alertController.preferredStyle) {
4343
case TYAlertControllerStyleAlert:
4444
alertController.alertView.alpha = 1.0;
@@ -60,7 +60,7 @@ - (void)dismissAnimateTransition:(id<UIViewControllerContextTransitioning>)trans
6060
TYAlertController *alertController = (TYAlertController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
6161

6262
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
63-
alertController.view.alpha = 0.0;
63+
alertController.backgroundView.alpha = 0.0;
6464
switch (alertController.preferredStyle) {
6565
case TYAlertControllerStyleAlert:
6666
alertController.alertView.alpha = 0.0;

TYAlertControllerDemo/TYAlertController/TYAlertView.m

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
@interface TYAlertAction ()
1313
@property (nonatomic, strong) NSString *title;
1414
@property (nonatomic, assign) TYAlertActionStyle style;
15+
@property (nonatomic, copy) void (^handler)(TYAlertAction *);
1516
@end
1617

1718
@implementation TYAlertAction
@@ -26,6 +27,7 @@ - (instancetype)initWithTitle:(NSString *)title style:(TYAlertActionStyle)style
2627
if (self = [super init]) {
2728
_title = title;
2829
_style = style;
30+
_enabled = YES;
2931

3032
}
3133
return self;
@@ -123,6 +125,7 @@ - (void)addContentViews
123125
_textFeildContentView = textFeildContentView;
124126

125127
UIView *buttonContentView = [[UIView alloc]init];
128+
buttonContentView.userInteractionEnabled = YES;
126129
[self addSubview:buttonContentView];
127130
_buttonContentView = buttonContentView;
128131
}
@@ -169,11 +172,7 @@ - (void)layoutContentViews
169172
// textContentView
170173
_textContentView.translatesAutoresizingMaskIntoConstraints = NO;
171174

172-
[self addConstraint:[NSLayoutConstraint constraintWithItem:_textContentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:_contentViewSpace]];
173-
174-
[self addConstraint:[NSLayoutConstraint constraintWithItem:_textContentView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1 constant:_contentViewEdge]];
175-
176-
[self addConstraint:[NSLayoutConstraint constraintWithItem:_textContentView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1 constant:-_contentViewEdge]];
175+
[self addConstarintWithView:_textContentView topView:self leftView:self bottomView:nil rightView:self edageInset:UIEdgeInsetsMake(_contentViewEdge, _contentViewEdge, 0, -_contentViewEdge)];
177176

178177
// // textFeildContentView
179178
// _textFeildContentView.translatesAutoresizingMaskIntoConstraints = NO;
@@ -184,33 +183,21 @@ - (void)layoutContentViews
184183
// [self addConstraint:[NSLayoutConstraint constraintWithItem:_textFeildContentView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1 constant:0]];
185184
//
186185
// buttonContentView
187-
_buttonContentView.translatesAutoresizingMaskIntoConstraints = NO;
188-
[self addConstraint:[NSLayoutConstraint constraintWithItem:_buttonContentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_textContentView attribute:NSLayoutAttributeBottom multiplier:1 constant:_contentViewSpace]];
189-
190-
[self addConstraint:[NSLayoutConstraint constraintWithItem:_buttonContentView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1 constant:_contentViewEdge]];
186+
_buttonContentView.translatesAutoresizingMaskIntoConstraints = NO;
191187

192-
[self addConstraint:[NSLayoutConstraint constraintWithItem:_buttonContentView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1 constant:-_contentViewEdge]];
188+
[self addConstarintWithTopView:_textContentView toBottomView:_buttonContentView constarint:_contentViewSpace];
193189

194-
[self addConstraint:[NSLayoutConstraint constraintWithItem:_buttonContentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:-_contentViewSpace]];
190+
[self addConstarintWithView:_buttonContentView topView:nil leftView:self bottomView:self rightView:self edageInset:UIEdgeInsetsMake(0, _contentViewEdge, -_contentViewEdge, -_contentViewEdge)];
195191
}
196192

197193
- (void)layoutTextLabels
198194
{
199195
_titleLable.translatesAutoresizingMaskIntoConstraints = NO;
200-
[_textContentView addConstraint:[NSLayoutConstraint constraintWithItem:_titleLable attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_textContentView attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
201-
202-
[_textContentView addConstraint:[NSLayoutConstraint constraintWithItem:_titleLable attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_textContentView attribute:NSLayoutAttributeLeft multiplier:1 constant:0]];
203-
204-
[_textContentView addConstraint:[NSLayoutConstraint constraintWithItem:_titleLable attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:_textContentView attribute:NSLayoutAttributeRight multiplier:1 constant:0]];
196+
[_textContentView addConstarintWithView:_titleLable topView:_textContentView leftView:_textContentView bottomView:nil rightView:_textContentView edageInset:UIEdgeInsetsZero];
205197

206198
_messageLabel.translatesAutoresizingMaskIntoConstraints = NO;
207-
[_textContentView addConstraint:[NSLayoutConstraint constraintWithItem:_messageLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_titleLable attribute:NSLayoutAttributeBottom multiplier:1 constant:_textLabelSpace]];
208-
209-
[_textContentView addConstraint:[NSLayoutConstraint constraintWithItem:_messageLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_textContentView attribute:NSLayoutAttributeLeft multiplier:1 constant:0]];
210-
211-
[_textContentView addConstraint:[NSLayoutConstraint constraintWithItem:_messageLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:_textContentView attribute:NSLayoutAttributeRight multiplier:1 constant:0]];
212-
213-
[_textContentView addConstraint:[NSLayoutConstraint constraintWithItem:_messageLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:_textContentView attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
199+
[_textContentView addConstarintWithTopView:_titleLable toBottomView:_messageLabel constarint:_textLabelSpace];
200+
[_textContentView addConstarintWithView:_messageLabel topView:nil leftView:_textContentView bottomView:_textContentView rightView:_textContentView edageInset:UIEdgeInsetsZero];
214201
}
215202

216203
- (void)layoutButtons
@@ -248,7 +235,14 @@ - (void)layoutButtons
248235

249236
- (void)actionButtonClicked:(UIButton *)button
250237
{
238+
NSLog(@"actionButtonClicked tag:%ld",button.tag);
239+
TYAlertAction *action = _actions[button.tag - kButtonTagOffset];
240+
241+
if (action.handler) {
242+
action.handler(action);
243+
}
251244

245+
[self.viewController dismissViewControllerAnimated:YES completion:nil];
252246
}
253247

254248
/*

TYAlertControllerDemo/TYAlertController/UIView+TYAutoLayout.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ - (void)addConstraintCenterXToView:(UIView *)xView CenterYToView:(UIView *)yView
7474
}
7575

7676
if (yView) {
77-
[self addConstraint:[NSLayoutConstraint constraintWithItem:xView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
77+
[self addConstraint:[NSLayoutConstraint constraintWithItem:yView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
7878
}
7979
}
8080

TYAlertControllerDemo/ViewController.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,17 @@ - (IBAction)showAlertView:(id)sender {
2929
view.backgroundColor = [UIColor blueColor];
3030
TYAlertView *alertView = [TYAlertView alertViewWithTitle:@"TYAlertView" message:@"A message should be a short, complete sentence."];
3131
alertView.backgroundColor = [UIColor whiteColor];
32+
__typeof (self) __weak weakSelf = self;
3233
[alertView addAction:[TYAlertAction actionWithTitle:@"确定" style:TYAlertActionStyleDestructive handler:^(TYAlertAction *action) {
33-
34+
3435
}]];
3536
[alertView addAction:[TYAlertAction actionWithTitle:@"取消" style:TYAlertActionStyleCancle handler:^(TYAlertAction *action) {
3637

3738
}]];
38-
[alertView addAction:[TYAlertAction actionWithTitle:@"默认" style:TYAlertActionStyleDefault handler:^(TYAlertAction *action) {
39+
[alertView addAction:[TYAlertAction actionWithTitle:@"默认1" style:TYAlertActionStyleDefault handler:^(TYAlertAction *action) {
40+
41+
}]];
42+
[alertView addAction:[TYAlertAction actionWithTitle:@"默认2" style:TYAlertActionStyleDefault handler:^(TYAlertAction *action) {
3943

4044
}]];
4145
TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:alertView preferredStyle:TYAlertControllerStyleAlert];

0 commit comments

Comments
 (0)