2
2
// TYAlertController.m
3
3
// TYAlertControllerDemo
4
4
//
5
- // Created by SunYong on 15/9/1.
5
+ // Created by tanyang on 15/9/1.
6
6
// Copyright (c) 2015年 tanyang. All rights reserved.
7
7
//
8
8
@@ -19,6 +19,8 @@ @interface TYAlertController ()
19
19
20
20
@property (nonatomic , assign ) Class transitionAnimationClass;
21
21
22
+ @property (nonatomic , weak ) UITapGestureRecognizer *singleTap;
23
+
22
24
@end
23
25
24
26
@implementation TYAlertController
@@ -60,19 +62,36 @@ - (instancetype)initWithAlertView:(UIView *)alertView preferredStyle:(TYAlertCon
60
62
return self;
61
63
}
62
64
65
+ + (instancetype )alertControllerWithAlertView : (UIView *)alertView
66
+ {
67
+ return [[self alloc ]initWithAlertView:alertView
68
+ preferredStyle: TYAlertControllerStyleAlert
69
+ transitionAnimation: TYAlertTransitionAnimationFade
70
+ transitionAnimationClass: nil ];
71
+ }
72
+
63
73
+ (instancetype )alertControllerWithAlertView : (UIView *)alertView preferredStyle : (TYAlertControllerStyle)preferredStyle
64
74
{
65
- return [[self alloc ]initWithAlertView:alertView preferredStyle: preferredStyle transitionAnimation: TYAlertTransitionAnimationFade transitionAnimationClass: nil ];
75
+ return [[self alloc ]initWithAlertView:alertView
76
+ preferredStyle: preferredStyle
77
+ transitionAnimation: TYAlertTransitionAnimationFade
78
+ transitionAnimationClass: nil ];
66
79
}
67
80
68
81
+ (instancetype )alertControllerWithAlertView : (UIView *)alertView preferredStyle : (TYAlertControllerStyle)preferredStyle transitionAnimation : (TYAlertTransitionAnimation)transitionAnimation
69
82
{
70
- return [[self alloc ]initWithAlertView:alertView preferredStyle: preferredStyle transitionAnimation: transitionAnimation transitionAnimationClass: nil ];
83
+ return [[self alloc ]initWithAlertView:alertView
84
+ preferredStyle: preferredStyle
85
+ transitionAnimation: transitionAnimation
86
+ transitionAnimationClass: nil ];
71
87
}
72
88
73
89
+ (instancetype )alertControllerWithAlertView : (UIView *)alertView preferredStyle : (TYAlertControllerStyle)preferredStyle transitionAnimationClass : (Class )transitionAnimationClass
74
90
{
75
- return [[self alloc ]initWithAlertView:alertView preferredStyle: preferredStyle transitionAnimation: TYAlertTransitionAnimationCustom transitionAnimationClass: transitionAnimationClass];
91
+ return [[self alloc ]initWithAlertView:alertView
92
+ preferredStyle: preferredStyle
93
+ transitionAnimation: TYAlertTransitionAnimationCustom
94
+ transitionAnimationClass: transitionAnimationClass];
76
95
}
77
96
78
97
#pragma mark - life cycle
@@ -81,6 +100,7 @@ - (void)viewDidLoad {
81
100
[super viewDidLoad ];
82
101
// Do any additional setup after loading the view.
83
102
self.view .backgroundColor = [UIColor clearColor ];
103
+
84
104
[self addBackgroundView ];
85
105
86
106
[self addSingleTapGesture ];
@@ -91,36 +111,44 @@ - (void)viewDidLoad {
91
111
92
112
}
93
113
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;
101
- }
102
-
103
114
- (void )addBackgroundView
104
115
{
105
- self.backgroundView .frame = self.view .bounds ;
106
- [self .view addSubview: self .backgroundView];
116
+ UIView *backgroundView = [[UIView alloc ]init];
117
+ backgroundView.translatesAutoresizingMaskIntoConstraints = NO ;
118
+ backgroundView.backgroundColor = [UIColor colorWithRed: 0 green: 0 blue: 0 alpha: 0.4 ];
119
+ [self .view addSubview: backgroundView];
120
+ _backgroundView = backgroundView;
121
+ [_backgroundView addConstraintToView: self .view edageInset: UIEdgeInsetsZero];
107
122
}
108
123
109
124
- (void )addSingleTapGesture
110
125
{
111
- if (!self.backgoundTapEnable ) {
112
- return ;
113
- }
114
- // 点击删除
115
126
self.view .userInteractionEnabled = YES ;
116
- // 单指单击
127
+
117
128
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc ] initWithTarget: self action: @selector (singleTap: )];
118
- // 手指数
119
- singleTap.numberOfTouchesRequired = 1 ;
120
- // 点击次数
121
- singleTap.numberOfTapsRequired = 1 ;
122
- // 增加事件者响应者,
129
+ singleTap.enabled = _backgoundTapDismissEnable;
130
+
123
131
[self .backgroundView addGestureRecognizer: singleTap];
132
+ _singleTap = singleTap;
133
+ }
134
+
135
+ - (void )setBackgroundView : (UIView *)backgroundView
136
+ {
137
+ if (_backgroundView != backgroundView) {
138
+ [_backgroundView removeFromSuperview ];
139
+
140
+ [self .view addSubview: backgroundView];
141
+ backgroundView.translatesAutoresizingMaskIntoConstraints = NO ;
142
+ _backgroundView = backgroundView;
143
+ [_backgroundView addConstraintToView: self .view edageInset: UIEdgeInsetsZero];
144
+ [self addSingleTapGesture ];
145
+ }
146
+ }
147
+
148
+ - (void )setBackgoundTapDismissEnable : (BOOL )backgoundTapDismissEnable
149
+ {
150
+ _backgoundTapDismissEnable = backgoundTapDismissEnable;
151
+ _singleTap.enabled = backgoundTapDismissEnable;
124
152
}
125
153
126
154
- (void )configureController
@@ -129,7 +157,7 @@ - (void)configureController
129
157
self.definesPresentationContext = YES ;
130
158
self.modalPresentationStyle = UIModalPresentationCustom;
131
159
self.transitioningDelegate = self;
132
- _backgoundTapEnable = YES ;
160
+ _backgoundTapDismissEnable = YES ;
133
161
_alertViewEdging = 15 ;
134
162
}
135
163
@@ -163,7 +191,6 @@ - (void)layoutAlertStyleView
163
191
164
192
// top Y
165
193
if (_alertViewOriginY > 0 ) {
166
-
167
194
[self .view addConstarintWithView: _alertView topView: self .view leftView: nil bottomView: nil rightView: nil edageInset: UIEdgeInsetsMake (_alertViewOriginY, 0 , 0 , 0 )];
168
195
}else {
169
196
[self .view addConstraintCenterXToView: nil CenterYToView: _alertView];
@@ -202,11 +229,16 @@ - (void)layoutActionSheetStyleView
202
229
203
230
}
204
231
232
+ - (void )dismissViewControllerAnimated : (BOOL )animated
233
+ {
234
+ [self dismissViewControllerAnimated: YES completion: self .dismissComplete];
235
+ }
236
+
205
237
#pragma mark - action
206
238
207
239
- (void )singleTap : (UITapGestureRecognizer *)sender
208
240
{
209
- [self dismissViewControllerAnimated: YES completion: self .dismissComplete ];
241
+ [self dismissViewControllerAnimated: YES ];
210
242
}
211
243
212
244
- (void )didReceiveMemoryWarning {
0 commit comments