Skip to content

Commit d7ccf87

Browse files
12207480tanyang
authored andcommitted
auto layout , blur effects
1 parent 5c75351 commit d7ccf87

15 files changed

+477
-73
lines changed

TYAlertControllerDemo.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
B0873F0D1BD370C700D2F4D5 /* TYShowAlertView.m in Sources */ = {isa = PBXBuildFile; fileRef = B0873F0C1BD370C700D2F4D5 /* TYShowAlertView.m */; settings = {ASSET_TAGS = (); }; };
11+
D324A5701BD8D13200BCBC6F /* UIImage+ImageEffects.m in Sources */ = {isa = PBXBuildFile; fileRef = D324A56F1BD8D13200BCBC6F /* UIImage+ImageEffects.m */; settings = {ASSET_TAGS = (); }; };
1112
D35854241B9532D700B5FA1F /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = D35854231B9532D700B5FA1F /* main.m */; };
1213
D35854271B9532D700B5FA1F /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = D35854261B9532D700B5FA1F /* AppDelegate.m */; };
1314
D358542A1B9532D700B5FA1F /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = D35854291B9532D700B5FA1F /* ViewController.m */; };
@@ -38,6 +39,8 @@
3839
/* Begin PBXFileReference section */
3940
B0873F0B1BD370C700D2F4D5 /* TYShowAlertView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TYShowAlertView.h; sourceTree = "<group>"; };
4041
B0873F0C1BD370C700D2F4D5 /* TYShowAlertView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TYShowAlertView.m; sourceTree = "<group>"; };
42+
D324A56E1BD8D13200BCBC6F /* UIImage+ImageEffects.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+ImageEffects.h"; sourceTree = "<group>"; };
43+
D324A56F1BD8D13200BCBC6F /* UIImage+ImageEffects.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+ImageEffects.m"; sourceTree = "<group>"; };
4144
D358541E1B9532D700B5FA1F /* TYAlertControllerDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TYAlertControllerDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
4245
D35854221B9532D700B5FA1F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4346
D35854231B9532D700B5FA1F /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
@@ -110,6 +113,8 @@
110113
D35854A31B953F3D00B5FA1F /* TYAlertController */,
111114
D35854251B9532D700B5FA1F /* AppDelegate.h */,
112115
D35854261B9532D700B5FA1F /* AppDelegate.m */,
116+
D324A56E1BD8D13200BCBC6F /* UIImage+ImageEffects.h */,
117+
D324A56F1BD8D13200BCBC6F /* UIImage+ImageEffects.m */,
113118
D35854281B9532D700B5FA1F /* ViewController.h */,
114119
D35854291B9532D700B5FA1F /* ViewController.m */,
115120
D358542B1B9532D700B5FA1F /* Main.storyboard */,
@@ -287,6 +292,7 @@
287292
buildActionMask = 2147483647;
288293
files = (
289294
D358542A1B9532D700B5FA1F /* ViewController.m in Sources */,
295+
D324A5701BD8D13200BCBC6F /* UIImage+ImageEffects.m in Sources */,
290296
D35854B51B95B7BB00B5FA1F /* TYBaseAnimation.m in Sources */,
291297
D3C1A9761B9D1A83009738B0 /* TYAlertView.m in Sources */,
292298
D37A91E11B9E7B6000C6661A /* UIView+TYAutoLayout.m in Sources */,

TYAlertControllerDemo/TYAlertController/TYAlertController.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ typedef NS_ENUM(NSInteger, TYAlertTransitionAnimation) {
3333

3434
@property (nonatomic, assign, readonly) Class transitionAnimationClass;
3535

36-
@property (nonatomic, assign) BOOL backgoundTapDismissEnable; // default YES
36+
@property (nonatomic, assign) BOOL backgoundTapDismissEnable; // default NO
3737

3838
@property (nonatomic, assign) CGFloat alertViewOriginY; // default center Y
3939

TYAlertControllerDemo/TYAlertController/TYAlertController.m

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ @interface TYAlertController ()
2121

2222
@property (nonatomic, weak) UITapGestureRecognizer *singleTap;
2323

24+
@property (nonatomic, strong) NSLayoutConstraint *alertViewCenterYConstraint;
25+
26+
@property (nonatomic, assign) CGFloat alertViewCenterYOffset;
27+
2428
@end
2529

2630
@implementation TYAlertController
@@ -109,15 +113,23 @@ - (void)viewDidLoad {
109113

110114
[self.view layoutIfNeeded];
111115

116+
if (_preferredStyle == TYAlertControllerStyleAlert) {
117+
// UIKeyboard Notification
118+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
119+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
120+
}
121+
112122
}
113123

114124
- (void)addBackgroundView
115125
{
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;
126+
if (_backgroundView == nil) {
127+
UIView *backgroundView = [[UIView alloc]init];
128+
backgroundView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
129+
_backgroundView = backgroundView;
130+
}
131+
_backgroundView.translatesAutoresizingMaskIntoConstraints = NO;
132+
[self.view addSubview:_backgroundView];
121133
[self.view addConstraintToView:_backgroundView edageInset:UIEdgeInsetsZero];
122134
}
123135

@@ -132,19 +144,6 @@ - (void)addSingleTapGesture
132144
_singleTap = singleTap;
133145
}
134146

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-
[self.view addConstraintToView:_backgroundView edageInset:UIEdgeInsetsZero];
144-
[self addSingleTapGesture];
145-
}
146-
}
147-
148147
- (void)setBackgoundTapDismissEnable:(BOOL)backgoundTapDismissEnable
149148
{
150149
_backgoundTapDismissEnable = backgoundTapDismissEnable;
@@ -157,7 +156,7 @@ - (void)configureController
157156
self.definesPresentationContext = YES;
158157
self.modalPresentationStyle = UIModalPresentationCustom;
159158
self.transitioningDelegate = self;
160-
_backgoundTapDismissEnable = YES;
159+
_backgoundTapDismissEnable = NO;
161160
_alertViewEdging = 15;
162161
}
163162

@@ -189,15 +188,8 @@ - (void)layoutAlertStyleView
189188
// center X
190189
[self.view addConstraintCenterXToView:_alertView CenterYToView:nil];
191190

192-
// top Y
193-
if (_alertViewOriginY > 0) {
194-
[self.view addConstarintWithView:_alertView topView:self.view leftView:nil bottomView:nil rightView:nil edageInset:UIEdgeInsetsMake(_alertViewOriginY, 0, 0, 0)];
195-
}else {
196-
[self.view addConstraintCenterXToView:nil CenterYToView:_alertView];
197-
}
198-
191+
// width, height
199192
if (!CGSizeEqualToSize(_alertView.frame.size,CGSizeZero)) {
200-
// width
201193
[_alertView addConstarintWidth:CGRectGetWidth(_alertView.frame) height:CGRectGetHeight(_alertView.frame)];
202194

203195
}else {
@@ -213,6 +205,17 @@ - (void)layoutAlertStyleView
213205
[_alertView addConstarintWidth:CGRectGetWidth(self.view.frame)-2*_alertViewEdging height:0];
214206
}
215207
}
208+
209+
// top Y
210+
_alertViewCenterYConstraint = [self.view addConstraintCenterYToView:_alertView constant:0];
211+
212+
if (_alertViewOriginY > 0) {
213+
[_alertView layoutIfNeeded];
214+
_alertViewCenterYOffset = _alertViewOriginY - (CGRectGetHeight(self.view.frame) - CGRectGetHeight(_alertView.frame))/2;
215+
_alertViewCenterYConstraint.constant = _alertViewCenterYOffset;
216+
}else{
217+
_alertViewCenterYOffset = 0;
218+
}
216219
}
217220

218221
- (void)layoutActionSheetStyleView
@@ -241,13 +244,39 @@ - (void)singleTap:(UITapGestureRecognizer *)sender
241244
[self dismissViewControllerAnimated:YES];
242245
}
243246

247+
#pragma mark - notifycation
248+
249+
- (void)keyboardWillShow:(NSNotification*)notification{
250+
CGRect keyboardRect = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
251+
252+
CGFloat alertViewBottomEdge = CGRectGetHeight(self.view.frame) - CGRectGetMaxY(_alertView.frame);
253+
CGFloat differ = CGRectGetHeight(keyboardRect) - alertViewBottomEdge;
254+
255+
if (differ > 0) {
256+
_alertViewCenterYConstraint.constant = _alertViewCenterYOffset - differ;
257+
[UIView animateWithDuration:0.25 animations:^{
258+
[self.view layoutIfNeeded];
259+
}];
260+
}
261+
}
262+
263+
- (void)keyboardWillHide:(NSNotification*)notification{
264+
265+
_alertViewCenterYConstraint.constant = _alertViewCenterYOffset;
266+
[UIView animateWithDuration:0.25 animations:^{
267+
[self.view layoutIfNeeded];
268+
}];
269+
}
270+
244271
- (void)didReceiveMemoryWarning {
245272
[super didReceiveMemoryWarning];
246273
// Dispose of any resources that can be recreated.
247274
}
248275

249276
- (void)dealloc
250277
{
278+
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
279+
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
251280
NSLog(@"TYAlertController dealloc");
252281
}
253282

TYAlertControllerDemo/TYAlertController/TYAlertFadeAnimation.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ - (void)presentAnimateTransition:(id<UIViewControllerContextTransitioning>)trans
2727
switch (alertController.preferredStyle) {
2828
case TYAlertControllerStyleAlert:
2929
alertController.alertView.alpha = 0.0;
30-
alertController.alertView.center = alertController.view.center;
3130
alertController.alertView.transform = CGAffineTransformMakeScale(0.5, 0.5);
3231
break;
3332
case TYAlertControllerStyleActionSheet:

TYAlertControllerDemo/TYAlertController/TYAlertScaleFadeAnimation.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ @implementation TYAlertScaleFadeAnimation
1212

1313
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
1414
{
15-
return 0.35;
15+
return 0.3;
1616
}
1717

1818
- (void)presentAnimateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
@@ -24,7 +24,6 @@ - (void)presentAnimateTransition:(id<UIViewControllerContextTransitioning>)trans
2424
switch (alertController.preferredStyle) {
2525
case TYAlertControllerStyleAlert:
2626
alertController.alertView.alpha = 0.0;
27-
alertController.alertView.center = alertController.view.center;
2827
alertController.alertView.transform = CGAffineTransformMakeScale(0.1, 0.1);
2928
break;
3029
case TYAlertControllerStyleActionSheet:

TYAlertControllerDemo/TYAlertController/TYAlertView.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ - (void)configureProperty
127127
_buttonSpace = kButtonSpace;
128128
_buttonContentViewEdge = kContentViewEdge;
129129
_buttonCornerRadius = 4.0;
130-
_buttonFont = [UIFont fontWithName:@"HelveticaNeue" size:16];;
130+
_buttonFont = [UIFont fontWithName:@"HelveticaNeue" size:18];;
131131
_buttonDefaultBgColor = [UIColor colorWithRed:52/255.0 green:152/255.0 blue:219/255.0 alpha:1];
132132
_buttonCancleBgColor = [UIColor colorWithRed:127/255.0 green:140/255.0 blue:141/255.0 alpha:1];
133133
_buttonDestructiveBgColor = [UIColor colorWithRed:231/255.0 green:76/255.0 blue:60/255.0 alpha:1];

TYAlertControllerDemo/TYAlertController/TYShowAlertView.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@
1010

1111
@interface TYShowAlertView : UIView
1212

13-
@property (nonatomic, weak, readonly) UIView *tipView;
13+
@property (nonatomic, weak, readonly) UIView *alertView;
1414
@property (nonatomic, weak) UIView *backgroundView;
1515

16-
@property (nonatomic, assign) BOOL backgoundTapDismissEnable; // default YES
16+
@property (nonatomic, assign) BOOL backgoundTapDismissEnable; // default NO
1717
@property (nonatomic, assign) CGFloat alertViewOriginY; // default center Y
1818
@property (nonatomic, assign) CGFloat alertViewEdging; // default 15
1919

2020

2121
+(void)showAlertViewWithView:(UIView *)alertView;
2222

23+
+ (void)showAlertViewWithView:(UIView *)alertView backgoundTapDismissEnable:(BOOL)backgoundTapDismissEnable;
24+
2325
+(void)showAlertViewWithView:(UIView *)alertView originY:(CGFloat)originY;
2426

2527
+(void)showAlertViewWithView:(UIView *)alertView originY:(CGFloat)originY backgoundTapDismissEnable:(BOOL)backgoundTapDismissEnable;

TYAlertControllerDemo/TYAlertController/TYShowAlertView.m

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#import "UIView+TYAutoLayout.h"
1111

1212
@interface TYShowAlertView ()
13-
@property (nonatomic, weak) UIView *tipView;
13+
@property (nonatomic, weak) UIView *alertView;
1414
@property (nonatomic, weak) UITapGestureRecognizer *singleTap;
1515
@end
1616

@@ -24,7 +24,7 @@ - (instancetype)initWithFrame:(CGRect)frame
2424
if (self = [super initWithFrame:frame]) {
2525
self.backgroundColor = [UIColor clearColor];
2626

27-
_backgoundTapDismissEnable = YES;
27+
_backgoundTapDismissEnable = NO;
2828
_alertViewEdging = 15;
2929

3030
[self addBackgroundView];
@@ -36,11 +36,10 @@ - (instancetype)initWithFrame:(CGRect)frame
3636

3737
- (instancetype)initWithAlertView:(UIView *)tipView
3838
{
39-
//[[UIScreen mainScreen] bounds]
40-
if (self = [self init]) {
39+
if (self = [self initWithFrame:CGRectZero]) {
4140

4241
[self addSubview:tipView];
43-
_tipView = tipView;
42+
_alertView = tipView;
4443
}
4544
return self;
4645
}
@@ -51,15 +50,21 @@ + (instancetype)alertViewWithView:(UIView *)tipView
5150
}
5251

5352
+ (void)showAlertViewWithView:(UIView *)alertView
53+
{
54+
[self showAlertViewWithView:alertView backgoundTapDismissEnable:NO];
55+
}
56+
57+
+ (void)showAlertViewWithView:(UIView *)alertView backgoundTapDismissEnable:(BOOL)backgoundTapDismissEnable
5458
{
5559
TYShowAlertView *showTipView = [self alertViewWithView:alertView];
60+
showTipView.backgoundTapDismissEnable = backgoundTapDismissEnable;
5661
[showTipView show];
5762
}
5863

5964
+ (void)showAlertViewWithView:(UIView *)alertView originY:(CGFloat)originY
6065
{
6166
[self showAlertViewWithView:alertView
62-
originY:originY backgoundTapDismissEnable:YES];
67+
originY:originY backgoundTapDismissEnable:NO];
6368
}
6469

6570
+ (void)showAlertViewWithView:(UIView *)alertView originY:(CGFloat)originY backgoundTapDismissEnable:(BOOL)backgoundTapDismissEnable
@@ -72,12 +77,14 @@ + (void)showAlertViewWithView:(UIView *)alertView originY:(CGFloat)originY backg
7277

7378
- (void)addBackgroundView
7479
{
75-
UIView *backgroundView = [[UIView alloc]initWithFrame:self.bounds];
76-
backgroundView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
77-
[self addSubview:backgroundView];
78-
_backgroundView = backgroundView;
79-
_backgroundView.translatesAutoresizingMaskIntoConstraints = NO;
80-
[self addConstraintToView:_backgroundView edageInset:UIEdgeInsetsZero];
80+
if (_backgroundView == nil) {
81+
UIView *backgroundView = [[UIView alloc]initWithFrame:self.bounds];
82+
backgroundView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
83+
[self addSubview:backgroundView];
84+
_backgroundView = backgroundView;
85+
_backgroundView.translatesAutoresizingMaskIntoConstraints = NO;
86+
[self addConstraintToView:_backgroundView edageInset:UIEdgeInsetsZero];
87+
}
8188
}
8289

8390
- (void)setBackgroundView:(UIView *)backgroundView
@@ -100,34 +107,36 @@ - (void)setBackgoundTapDismissEnable:(BOOL)backgoundTapDismissEnable
100107

101108
- (void)layoutAlertView
102109
{
103-
_tipView.translatesAutoresizingMaskIntoConstraints = NO;
110+
//[self layoutIfNeeded];
111+
_alertView.translatesAutoresizingMaskIntoConstraints = NO;
104112
// center X
105-
[self addConstraintCenterXToView:_tipView CenterYToView:nil];
113+
[self addConstraintCenterXToView:_alertView CenterYToView:nil];
106114

107-
// top Y
108-
if (_alertViewOriginY > 0) {
109-
[self addConstarintWithView:_tipView topView:self leftView:nil bottomView:nil rightView:nil edageInset:UIEdgeInsetsMake(_alertViewOriginY, 0, 0, 0)];
110-
}else {
111-
[self addConstraintCenterXToView:nil CenterYToView:_tipView];
112-
}
113-
114-
if (!CGSizeEqualToSize(_tipView.frame.size,CGSizeZero)) {
115-
// width
116-
[_tipView addConstarintWidth:CGRectGetWidth(_tipView.frame) height:CGRectGetHeight(_tipView.frame)];
115+
// width, height
116+
if (!CGSizeEqualToSize(_alertView.frame.size,CGSizeZero)) {
117+
[_alertView addConstarintWidth:CGRectGetWidth(_alertView.frame) height:CGRectGetHeight(_alertView.frame)];
117118

118119
}else {
119120
BOOL findAlertViewWidthConstraint = NO;
120-
for (NSLayoutConstraint *constraint in _tipView.constraints) {
121+
for (NSLayoutConstraint *constraint in _alertView.constraints) {
121122
if (constraint.firstAttribute == NSLayoutAttributeWidth) {
122123
findAlertViewWidthConstraint = YES;
123124
break;
124125
}
125126
}
126127

127128
if (!findAlertViewWidthConstraint) {
128-
[_tipView addConstarintWidth:CGRectGetWidth(self.frame)-2*_alertViewEdging height:0];
129+
[_alertView addConstarintWidth:CGRectGetWidth(kCurrentWindow.frame)-2*_alertViewEdging height:0];
129130
}
130131
}
132+
133+
// topY
134+
NSLayoutConstraint *alertViewCenterYConstraint = [self addConstraintCenterYToView:_alertView constant:0];
135+
136+
if (_alertViewOriginY > 0) {
137+
[_alertView layoutIfNeeded];
138+
alertViewCenterYConstraint.constant = _alertViewOriginY - (CGRectGetHeight(kCurrentWindow.frame) - CGRectGetHeight(_alertView.frame))/2;
139+
}
131140
}
132141

133142
#pragma mark - add Gesture
@@ -152,14 +161,14 @@ - (void)show
152161
{
153162
if (self.superview == nil) {
154163
[kCurrentWindow addSubview:self];
155-
self.translatesAutoresizingMaskIntoConstraints = 0;
164+
self.translatesAutoresizingMaskIntoConstraints = NO;
156165
[kCurrentWindow addConstraintToView:self edageInset:UIEdgeInsetsZero];
157166
[self layoutAlertView];
158167
}
159168
self.alpha = 0;
160-
self.tipView.transform = CGAffineTransformScale(self.tipView.transform,0.1,0.1);
169+
_alertView.transform = CGAffineTransformScale(_alertView.transform,0.1,0.1);
161170
[UIView animateWithDuration:0.3 animations:^{
162-
self.tipView.transform = CGAffineTransformIdentity;
171+
_alertView.transform = CGAffineTransformIdentity;
163172
self.alpha = 1;
164173
}];
165174

@@ -169,7 +178,7 @@ - (void)hide
169178
{
170179
if (self.superview) {
171180
[UIView animateWithDuration:0.3 animations:^{
172-
self.tipView.transform = CGAffineTransformScale(self.tipView.transform,0.1,0.1);
181+
_alertView.transform = CGAffineTransformScale(_alertView.transform,0.1,0.1);
173182
self.alpha = 0;
174183
} completion:^(BOOL finished) {
175184
[self removeFromSuperview];

0 commit comments

Comments
 (0)