Skip to content

Commit 205d42b

Browse files
committed
optimized naming
1 parent 2e8c685 commit 205d42b

File tree

7 files changed

+149
-45
lines changed

7 files changed

+149
-45
lines changed

TYAlertControllerDemo/Base.lproj/Main.storyboard

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</userDefinedRuntimeAttributes>
2828
</imageView>
2929
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Yfr-BX-DRT">
30-
<rect key="frame" x="237" y="237" width="127" height="30"/>
30+
<rect key="frame" x="237" y="270" width="127" height="30"/>
3131
<animations/>
3232
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
3333
<constraints>
@@ -72,11 +72,11 @@
7272
</mask>
7373
</variation>
7474
<connections>
75-
<action selector="showAlertView:" destination="BYZ-38-t0r" eventType="touchUpInside" id="N4f-R7-iby"/>
75+
<action selector="showAlertViewAction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="KD8-s9-03g"/>
7676
</connections>
7777
</button>
7878
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Pbc-5y-GLg">
79-
<rect key="frame" x="255" y="165" width="91" height="30"/>
79+
<rect key="frame" x="255" y="158" width="91" height="30"/>
8080
<animations/>
8181
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
8282
<constraints>
@@ -92,11 +92,11 @@
9292
</userDefinedRuntimeAttribute>
9393
</userDefinedRuntimeAttributes>
9494
<connections>
95-
<action selector="showActionSheet:" destination="BYZ-38-t0r" eventType="touchUpInside" id="6ok-Ne-kuo"/>
95+
<action selector="showActionSheetAction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="2zn-h5-WOy"/>
9696
</connections>
9797
</button>
9898
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="1WY-V3-iTv">
99-
<rect key="frame" x="229.5" y="317" width="142" height="30"/>
99+
<rect key="frame" x="229" y="350" width="142" height="30"/>
100100
<animations/>
101101
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
102102
<constraints>
@@ -110,7 +110,33 @@
110110
</userDefinedRuntimeAttribute>
111111
</userDefinedRuntimeAttributes>
112112
<connections>
113-
<action selector="showAlertViewInWindow:" destination="BYZ-38-t0r" eventType="touchUpInside" id="PKv-Os-aIC"/>
113+
<action selector="showAlertViewInWindowAction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="JP9-Fw-wZs"/>
114+
</connections>
115+
</button>
116+
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="4z7-Ic-djO">
117+
<rect key="frame" x="230" y="214" width="140" height="30"/>
118+
<animations/>
119+
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
120+
<constraints>
121+
<constraint firstAttribute="height" constant="30" id="7W3-g0-HlF"/>
122+
<constraint firstAttribute="width" constant="91" id="X9c-NO-6FL"/>
123+
<constraint firstAttribute="width" constant="140" id="wO9-1P-dv3"/>
124+
</constraints>
125+
<state key="normal" title="blurEffectAlertView">
126+
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
127+
</state>
128+
<userDefinedRuntimeAttributes>
129+
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
130+
<integer key="value" value="5"/>
131+
</userDefinedRuntimeAttribute>
132+
</userDefinedRuntimeAttributes>
133+
<variation key="default">
134+
<mask key="constraints">
135+
<exclude reference="X9c-NO-6FL"/>
136+
</mask>
137+
</variation>
138+
<connections>
139+
<action selector="blurEffectAlertViewAction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="yMK-PD-pF4"/>
114140
</connections>
115141
</button>
116142
</subviews>
@@ -121,16 +147,24 @@
121147
<constraint firstAttribute="trailing" secondItem="k0k-Qy-0Gy" secondAttribute="trailing" id="8ry-Au-szq"/>
122148
<constraint firstItem="Yfr-BX-DRT" firstAttribute="top" secondItem="Pbc-5y-GLg" secondAttribute="bottom" constant="42" id="9NA-YE-ipl"/>
123149
<constraint firstItem="k0k-Qy-0Gy" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="Abd-iC-uZ3"/>
150+
<constraint firstItem="4z7-Ic-djO" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="Avw-oo-VhF"/>
124151
<constraint firstItem="k0k-Qy-0Gy" firstAttribute="top" secondItem="8bC-Xf-vdC" secondAttribute="top" id="N7f-Z7-qps"/>
125152
<constraint firstItem="Yfr-BX-DRT" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="SDo-Pv-z7k"/>
153+
<constraint firstItem="4z7-Ic-djO" firstAttribute="top" secondItem="Pbc-5y-GLg" secondAttribute="bottom" constant="26" id="T0j-64-O2J"/>
126154
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="k0k-Qy-0Gy" secondAttribute="bottom" id="VGn-hG-lI4"/>
127-
<constraint firstItem="Pbc-5y-GLg" firstAttribute="top" secondItem="vCS-SU-E0R" secondAttribute="bottom" constant="35" id="XMM-Dk-17g"/>
155+
<constraint firstItem="Yfr-BX-DRT" firstAttribute="top" secondItem="4z7-Ic-djO" secondAttribute="bottom" constant="26" id="WK2-CA-DMO"/>
156+
<constraint firstItem="Pbc-5y-GLg" firstAttribute="top" secondItem="vCS-SU-E0R" secondAttribute="bottom" constant="28" id="XMM-Dk-17g"/>
128157
<constraint firstItem="k0k-Qy-0Gy" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" id="g1Y-r6-VZe"/>
129158
<constraint firstItem="1WY-V3-iTv" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="g2R-k6-tZu"/>
130159
<constraint firstItem="vCS-SU-E0R" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="80" id="gTK-jF-eL0"/>
131160
<constraint firstAttribute="centerX" secondItem="Pbc-5y-GLg" secondAttribute="centerX" id="mBu-C3-nZu"/>
132161
<constraint firstAttribute="centerX" secondItem="vCS-SU-E0R" secondAttribute="centerX" id="tYU-co-tHf"/>
133162
</constraints>
163+
<variation key="default">
164+
<mask key="constraints">
165+
<exclude reference="9NA-YE-ipl"/>
166+
</mask>
167+
</variation>
134168
</view>
135169
</viewController>
136170
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>

TYAlertControllerDemo/TYAlertController/TYAlertController.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@ - (void)layoutActionSheetStyleView
229229
// height
230230
[_alertView addConstarintWidth:0 height:CGRectGetHeight(_alertView.frame)];
231231
}
232-
233232
}
234233

235234
- (void)dismissViewControllerAnimated:(BOOL)animated

TYAlertControllerDemo/TYAlertController/TYAlertView.m

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ - (instancetype)initWithTitle:(NSString *)title style:(TYAlertActionStyle)style
2828
if (self = [super init]) {
2929
_title = title;
3030
_style = style;
31+
_handler = handler;
3132
_enabled = YES;
3233

3334
}
@@ -59,6 +60,7 @@ @interface TYAlertView ()
5960

6061
// button content View
6162
@property (nonatomic, weak) UIView *buttonContentView;
63+
@property (nonatomic, weak) NSLayoutConstraint *buttonTopConstraint;
6264
@property (nonatomic, strong) NSMutableArray *buttons;
6365
@property (nonatomic, strong) NSMutableArray *actions;
6466

@@ -221,6 +223,11 @@ - (void)addAction:(TYAlertAction *)action
221223
[_buttons addObject:button];
222224
[_actions addObject:action];
223225

226+
if (_buttons.count == 1) {
227+
[self layoutContentViews];
228+
[self layoutTextLabels];
229+
}
230+
224231
[self layoutButtons];
225232
}
226233

@@ -261,6 +268,7 @@ - (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textFeild))c
261268
- (void)layoutContentViews
262269
{
263270
if (!_textContentView.translatesAutoresizingMaskIntoConstraints) {
271+
// layout done
264272
return;
265273
}
266274
// textContentView
@@ -277,14 +285,15 @@ - (void)layoutContentViews
277285
// buttonContentView
278286
_buttonContentView.translatesAutoresizingMaskIntoConstraints = NO;
279287

280-
[self addConstarintWithTopView:_textFeildContentView toBottomView:_buttonContentView constarint:_contentViewSpace];
288+
_buttonTopConstraint = [self addConstarintWithTopView:_textFeildContentView toBottomView:_buttonContentView constarint:0];
281289

282290
[self addConstarintWithView:_buttonContentView topView:nil leftView:self bottomView:self rightView:self edageInset:UIEdgeInsetsMake(0, _buttonContentViewEdge, -_contentViewSpace, -_buttonContentViewEdge)];
283291
}
284292

285293
- (void)layoutTextLabels
286294
{
287295
if (!_titleLable.translatesAutoresizingMaskIntoConstraints && !_messageLabel.translatesAutoresizingMaskIntoConstraints) {
296+
// layout done
288297
return;
289298
}
290299
// title
@@ -301,6 +310,7 @@ - (void)layoutButtons
301310
{
302311
UIButton *button = _buttons.lastObject;
303312
if (_buttons.count == 1) {
313+
_buttonTopConstraint.constant = -_contentViewSpace;
304314
[_buttonContentView addConstraintToView:button edageInset:UIEdgeInsetsZero];
305315
[button addConstarintWidth:0 height:_buttonHeight];
306316
}else if (_buttons.count == 2) {
@@ -369,7 +379,7 @@ - (void)actionButtonClicked:(UIButton *)button
369379
action.handler(action);
370380
}
371381

372-
[self.viewController dismissViewControllerAnimated:YES completion:nil];
382+
[self hideView];
373383
}
374384

375385
- (void)dealloc

TYAlertControllerDemo/TYAlertController/UIView+TYAlertView.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131

3232
- (void)showInController:(UIViewController *)viewController preferredStyle:(TYAlertControllerStyle)preferredStyle transitionAnimation:(TYAlertTransitionAnimation)transitionAnimation backgoundTapDismissEnable:(BOOL)backgoundTapDismissEnable;
3333

34-
- (void)hideInController;
35-
3634
#pragma mark - show in window
3735

3836
- (void)showInWindow;
@@ -44,6 +42,14 @@
4442

4543
- (void)showInWindowWithOriginY:(CGFloat)OriginY backgoundTapDismissEnable:(BOOL)backgoundTapDismissEnable;
4644

45+
46+
#pragma mark - hide
47+
48+
// this will judge and call right method
49+
- (void)hideView;
50+
51+
- (void)hideInController;
52+
4753
- (void)hideInWindow;
4854

4955
@end

TYAlertControllerDemo/TYAlertController/UIView+TYAlertView.m

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ - (void)showInWindowWithOriginY:(CGFloat)OriginY backgoundTapDismissEnable:(BOOL
6262

6363
- (void)hideInWindow
6464
{
65-
if (self.superview && [self.superview isKindOfClass:[TYShowAlertView class]]) {
65+
if ([self isShowInWindow]) {
6666
[(TYShowAlertView *)self.superview hide];
6767
}else {
6868
NSLog(@"self.superview is nil, or isn't TYShowAlertView");
@@ -103,12 +103,42 @@ - (void)showInController:(UIViewController *)viewController preferredStyle:(TYAl
103103
}
104104

105105
- (void)hideInController
106+
{
107+
if ([self isShowInAlertController]) {
108+
[(TYAlertController *)self.viewController dismissViewControllerAnimated:YES];
109+
}else {
110+
NSLog(@"self.viewController is nil, or isn't TYAlertController");
111+
}
112+
}
113+
114+
#pragma mark - hide
115+
116+
- (BOOL)isShowInAlertController
106117
{
107118
UIViewController *viewController = self.viewController;
108119
if (viewController && [viewController isKindOfClass:[TYAlertController class]]) {
109-
[(TYAlertController *)viewController dismissViewControllerAnimated:YES];
120+
return YES;
121+
}
122+
return NO;
123+
124+
}
125+
126+
- (BOOL)isShowInWindow
127+
{
128+
if (self.superview && [self.superview isKindOfClass:[TYShowAlertView class]]) {
129+
return YES;
130+
}
131+
return NO;
132+
}
133+
134+
- (void)hideView
135+
{
136+
if ([self isShowInAlertController]) {
137+
[self hideInController];
138+
}else if ([self isShowInWindow]) {
139+
[self hideInWindow];
110140
}else {
111-
NSLog(@"self.viewController is nil, or isn't TYAlertController");
141+
NSLog(@"self.viewController is nil, or isn't TYAlertController,or self.superview is nil, or isn't TYShowAlertView");
112142
}
113143
}
114144

TYAlertControllerDemo/UIImage+ImageEffects.m

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ @implementation UIImage (ImageEffects)
105105
- (UIImage *)applyLightEffect
106106
{
107107
UIColor *tintColor = [UIColor colorWithWhite:1.0 alpha:0.3];
108-
return [self applyBlurWithRadius:20 tintColor:tintColor saturationDeltaFactor:1.8 maskImage:nil];
108+
return [self applyBlurWithRadius:10 tintColor:tintColor saturationDeltaFactor:1.8 maskImage:nil];
109109
}
110110

111111

@@ -280,11 +280,14 @@ @implementation UIImage (SnapshotImage)
280280

281281
+ (UIImage *)snapshotImageWithView:(UIView *)view
282282
{
283-
UIGraphicsBeginImageContext(view.bounds.size); //currentView 当前的view 创建一个基于位图的图形上下文并指定大小为
284-
[view.layer renderInContext:UIGraphicsGetCurrentContext()];//renderInContext呈现接受者及其子范围到指定的上下文
285-
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();//返回一个基于当前图形上下文的图片
286-
UIGraphicsEndImageContext();
283+
//currentView 当前的view 创建一个基于位图的图形上下文并指定大小为
284+
UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, [UIScreen mainScreen].scale);
285+
//renderInContext呈现接受者及其子范围到指定的上下文
286+
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
287+
//返回一个基于当前图形上下文的图片
288+
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
287289
// 生成后的image
290+
UIGraphicsEndImageContext();
288291

289292
return image;
290293
}

TYAlertControllerDemo/ViewController.m

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,53 +11,46 @@
1111
#import "UIImage+ImageEffects.h"
1212

1313
@interface ViewController ()
14-
1514
@end
1615

1716
@implementation ViewController
1817

1918
- (void)viewDidLoad {
2019
[super viewDidLoad];
2120
// Do any additional setup after loading the view, typically from a nib.
22-
//UIAlertController
2321
}
2422

2523

26-
- (IBAction)showAlertView:(id)sender {
24+
- (IBAction)showAlertViewAction:(id)sender {
2725

2826
TYAlertView *alertView = [TYAlertView alertViewWithTitle:@"TYAlertView" message:@"A message should be a short, complete sentence."];
29-
30-
__typeof (self) __weak weakSelf = self;
31-
27+
3228
[alertView addAction:[TYAlertAction actionWithTitle:@"取消" style:TYAlertActionStyleCancle handler:^(TYAlertAction *action) {
33-
29+
NSLog(@"%@",action.title);
30+
}]];
31+
32+
[alertView addAction:[TYAlertAction actionWithTitle:@"确定" style:TYAlertActionStyleDestructive handler:^(TYAlertAction *action) {
33+
NSLog(@"%@",action.title);
3434
}]];
35-
//
36-
// [alertView addAction:[TYAlertAction actionWithTitle:@"确定" style:TYAlertActionStyleDestructive handler:^(TYAlertAction *action) {
37-
//
38-
// }]];
3935

4036
// [alertView addAction:[TYAlertAction actionWithTitle:@"默认1" style:TYAlertActionStyleDefault handler:^(TYAlertAction *action) {
4137
//
4238
// }]];
4339

4440
//
45-
// [alertView addTextFieldWithConfigurationHandler:^(UITextField *textField) {
46-
//
47-
// }];
48-
// [alertView addTextFieldWithConfigurationHandler:^(UITextField *textField) {
49-
//
50-
// }];
41+
[alertView addTextFieldWithConfigurationHandler:^(UITextField *textField) {
42+
textField.placeholder = @"请输入账号";
43+
}];
44+
[alertView addTextFieldWithConfigurationHandler:^(UITextField *textField) {
45+
textField.placeholder = @"请输入密码";
46+
}];
5147

5248
TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:alertView preferredStyle:TYAlertControllerStyleAlert];
5349

54-
UIImage *image = [UIImage snapshotImageWithView:self.view];
55-
alertController.backgroundView = [[UIImageView alloc]initWithImage:[image applyLightEffect]];
56-
5750
//alertController.alertViewOriginY = 60;
5851
[self presentViewController:alertController animated:YES completion:nil];
5952
}
60-
- (IBAction)showActionSheet:(id)sender {
53+
- (IBAction)showActionSheetAction:(id)sender {
6154

6255
TYAlertView *alertView = [TYAlertView alertViewWithTitle:@"TYAlertView" message:@"A message should be a short, complete sentence."];
6356

@@ -79,13 +72,42 @@ - (IBAction)showActionSheet:(id)sender {
7972
TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:alertView preferredStyle:TYAlertControllerStyleActionSheet];
8073
[self presentViewController:alertController animated:YES completion:nil];
8174
}
82-
- (IBAction)showAlertViewInWindow:(id)sender {
75+
76+
- (IBAction)blurEffectAlertViewAction:(id)sender {
77+
TYAlertView *alertView = [TYAlertView alertViewWithTitle:@"TYAlertView" message:@"This is a blur effect on background, is beautiful effect"];
8378

84-
UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
85-
redView.backgroundColor = [UIColor redColor];
79+
[alertView addAction:[TYAlertAction actionWithTitle:@"取消" style:TYAlertActionStyleCancle handler:^(TYAlertAction *action) {
80+
81+
}]];
82+
83+
TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:alertView preferredStyle:TYAlertControllerStyleAlert];
84+
85+
UIImage *blurImage =[[UIImage snapshotImageWithView:self.view] applyLightEffect];
86+
alertController.backgroundView = [[UIImageView alloc]initWithImage:blurImage];
87+
88+
//alertController.alertViewOriginY = 60;
89+
[self presentViewController:alertController animated:YES completion:nil];
90+
}
91+
92+
93+
- (IBAction)showAlertViewInWindowAction:(id)sender {
94+
95+
// UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
96+
// redView.backgroundColor = [UIColor redColor];
8697
//[redView showInWindowWithBackgoundTapDismissEnable:YES];
8798
//[redView showInController:self];
88-
[TYShowAlertView showAlertViewWithView:redView originY:60 backgoundTapDismissEnable:YES];
99+
100+
TYAlertView *alertView = [TYAlertView alertViewWithTitle:@"TYAlertView" message:@"A message should be a short, complete sentence."];
101+
102+
[alertView addAction:[TYAlertAction actionWithTitle:@"取消" style:TYAlertActionStyleCancle handler:^(TYAlertAction *action) {
103+
104+
}]];
105+
106+
[alertView addAction:[TYAlertAction actionWithTitle:@"确定" style:TYAlertActionStyleDestructive handler:^(TYAlertAction *action) {
107+
108+
}]];
109+
110+
[TYShowAlertView showAlertViewWithView:alertView originY:200 backgoundTapDismissEnable:YES];
89111
}
90112

91113
- (void)didReceiveMemoryWarning {

0 commit comments

Comments
 (0)