Skip to content

Commit 2e8c685

Browse files
12207480tanyang
authored andcommitted
code reviewed ,optimization
1 parent d7ccf87 commit 2e8c685

File tree

8 files changed

+124
-47
lines changed

8 files changed

+124
-47
lines changed

TYAlertControllerDemo.xcodeproj/project.pbxproj

Lines changed: 4 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+
B08FD7D01BD905EF00B83FF5 /* screen.jpg in Resources */ = {isa = PBXBuildFile; fileRef = B08FD7CF1BD905EF00B83FF5 /* screen.jpg */; settings = {ASSET_TAGS = (); }; };
1112
D324A5701BD8D13200BCBC6F /* UIImage+ImageEffects.m in Sources */ = {isa = PBXBuildFile; fileRef = D324A56F1BD8D13200BCBC6F /* UIImage+ImageEffects.m */; settings = {ASSET_TAGS = (); }; };
1213
D35854241B9532D700B5FA1F /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = D35854231B9532D700B5FA1F /* main.m */; };
1314
D35854271B9532D700B5FA1F /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = D35854261B9532D700B5FA1F /* AppDelegate.m */; };
@@ -39,6 +40,7 @@
3940
/* Begin PBXFileReference section */
4041
B0873F0B1BD370C700D2F4D5 /* TYShowAlertView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TYShowAlertView.h; sourceTree = "<group>"; };
4142
B0873F0C1BD370C700D2F4D5 /* TYShowAlertView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TYShowAlertView.m; sourceTree = "<group>"; };
43+
B08FD7CF1BD905EF00B83FF5 /* screen.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = screen.jpg; sourceTree = "<group>"; };
4244
D324A56E1BD8D13200BCBC6F /* UIImage+ImageEffects.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+ImageEffects.h"; sourceTree = "<group>"; };
4345
D324A56F1BD8D13200BCBC6F /* UIImage+ImageEffects.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+ImageEffects.m"; sourceTree = "<group>"; };
4446
D358541E1B9532D700B5FA1F /* TYAlertControllerDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TYAlertControllerDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -128,6 +130,7 @@
128130
D35854211B9532D700B5FA1F /* Supporting Files */ = {
129131
isa = PBXGroup;
130132
children = (
133+
B08FD7CF1BD905EF00B83FF5 /* screen.jpg */,
131134
D35854221B9532D700B5FA1F /* Info.plist */,
132135
D35854231B9532D700B5FA1F /* main.m */,
133136
);
@@ -272,6 +275,7 @@
272275
buildActionMask = 2147483647;
273276
files = (
274277
D358542D1B9532D700B5FA1F /* Main.storyboard in Resources */,
278+
B08FD7D01BD905EF00B83FF5 /* screen.jpg in Resources */,
275279
D35854321B9532D700B5FA1F /* LaunchScreen.xib in Resources */,
276280
D358542F1B9532D700B5FA1F /* Images.xcassets in Resources */,
277281
);

TYAlertControllerDemo/Base.lproj/Main.storyboard

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,98 @@
1717
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
1818
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
1919
<subviews>
20+
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="screen.jpg" translatesAutoresizingMaskIntoConstraints="NO" id="k0k-Qy-0Gy">
21+
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
22+
<animations/>
23+
<userDefinedRuntimeAttributes>
24+
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
25+
<integer key="value" value="5"/>
26+
</userDefinedRuntimeAttribute>
27+
</userDefinedRuntimeAttributes>
28+
</imageView>
29+
<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"/>
31+
<animations/>
32+
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
33+
<constraints>
34+
<constraint firstAttribute="width" constant="91" id="HNK-ek-Ch4"/>
35+
<constraint firstAttribute="width" constant="127" id="gZw-9F-Seh"/>
36+
<constraint firstAttribute="height" constant="30" id="md1-KB-DEL"/>
37+
</constraints>
38+
<state key="normal" title="customAlertView">
39+
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
40+
</state>
41+
<userDefinedRuntimeAttributes>
42+
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
43+
<integer key="value" value="5"/>
44+
</userDefinedRuntimeAttribute>
45+
</userDefinedRuntimeAttributes>
46+
<variation key="default">
47+
<mask key="constraints">
48+
<exclude reference="HNK-ek-Ch4"/>
49+
</mask>
50+
</variation>
51+
</button>
2052
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="vCS-SU-E0R">
21-
<rect key="frame" x="257" y="103" width="87" height="30"/>
53+
<rect key="frame" x="222" y="100" width="156" height="30"/>
2254
<animations/>
55+
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
2356
<constraints>
57+
<constraint firstAttribute="width" constant="156" id="AfD-4k-fI6"/>
2458
<constraint firstAttribute="width" constant="87" id="EDr-Az-Wg8"/>
2559
<constraint firstAttribute="height" constant="30" id="M2D-4m-vPv"/>
2660
</constraints>
27-
<state key="normal" title="AlertView">
61+
<state key="normal" title="AlertViewInController">
2862
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
2963
</state>
64+
<userDefinedRuntimeAttributes>
65+
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
66+
<integer key="value" value="5"/>
67+
</userDefinedRuntimeAttribute>
68+
</userDefinedRuntimeAttributes>
69+
<variation key="default">
70+
<mask key="constraints">
71+
<exclude reference="EDr-Az-Wg8"/>
72+
</mask>
73+
</variation>
3074
<connections>
3175
<action selector="showAlertView:" destination="BYZ-38-t0r" eventType="touchUpInside" id="N4f-R7-iby"/>
3276
</connections>
3377
</button>
3478
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Pbc-5y-GLg">
35-
<rect key="frame" x="255" y="168" width="91" height="30"/>
79+
<rect key="frame" x="255" y="165" width="91" height="30"/>
3680
<animations/>
81+
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
3782
<constraints>
3883
<constraint firstAttribute="width" constant="91" id="gIB-6P-PPI"/>
3984
<constraint firstAttribute="height" constant="30" id="zbZ-eV-dCO"/>
4085
</constraints>
4186
<state key="normal" title="ActionSheet">
4287
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
4388
</state>
89+
<userDefinedRuntimeAttributes>
90+
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
91+
<integer key="value" value="5"/>
92+
</userDefinedRuntimeAttribute>
93+
</userDefinedRuntimeAttributes>
4494
<connections>
4595
<action selector="showActionSheet:" destination="BYZ-38-t0r" eventType="touchUpInside" id="6ok-Ne-kuo"/>
4696
</connections>
4797
</button>
4898
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="1WY-V3-iTv">
49-
<rect key="frame" x="231" y="240" width="139" height="30"/>
99+
<rect key="frame" x="229.5" y="317" width="142" height="30"/>
50100
<animations/>
101+
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
51102
<constraints>
52103
<constraint firstAttribute="height" constant="30" id="U3E-o2-zSc"/>
53-
<constraint firstAttribute="width" constant="139" id="eKz-vX-AMM"/>
104+
<constraint firstAttribute="width" constant="142" id="eKz-vX-AMM"/>
54105
</constraints>
55106
<state key="normal" title="AlertViewInWindow"/>
107+
<userDefinedRuntimeAttributes>
108+
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
109+
<integer key="value" value="5"/>
110+
</userDefinedRuntimeAttribute>
111+
</userDefinedRuntimeAttributes>
56112
<connections>
57113
<action selector="showAlertViewInWindow:" destination="BYZ-38-t0r" eventType="touchUpInside" id="PKv-Os-aIC"/>
58114
</connections>
@@ -61,10 +117,17 @@
61117
<animations/>
62118
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
63119
<constraints>
64-
<constraint firstItem="1WY-V3-iTv" firstAttribute="top" secondItem="Pbc-5y-GLg" secondAttribute="bottom" constant="42" id="437-WF-XhU"/>
120+
<constraint firstItem="1WY-V3-iTv" firstAttribute="top" secondItem="Yfr-BX-DRT" secondAttribute="bottom" constant="50" id="5eB-EE-r5Z"/>
121+
<constraint firstAttribute="trailing" secondItem="k0k-Qy-0Gy" secondAttribute="trailing" id="8ry-Au-szq"/>
122+
<constraint firstItem="Yfr-BX-DRT" firstAttribute="top" secondItem="Pbc-5y-GLg" secondAttribute="bottom" constant="42" id="9NA-YE-ipl"/>
123+
<constraint firstItem="k0k-Qy-0Gy" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="Abd-iC-uZ3"/>
124+
<constraint firstItem="k0k-Qy-0Gy" firstAttribute="top" secondItem="8bC-Xf-vdC" secondAttribute="top" id="N7f-Z7-qps"/>
125+
<constraint firstItem="Yfr-BX-DRT" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="SDo-Pv-z7k"/>
126+
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="k0k-Qy-0Gy" secondAttribute="bottom" id="VGn-hG-lI4"/>
65127
<constraint firstItem="Pbc-5y-GLg" firstAttribute="top" secondItem="vCS-SU-E0R" secondAttribute="bottom" constant="35" id="XMM-Dk-17g"/>
128+
<constraint firstItem="k0k-Qy-0Gy" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" id="g1Y-r6-VZe"/>
66129
<constraint firstItem="1WY-V3-iTv" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="g2R-k6-tZu"/>
67-
<constraint firstItem="vCS-SU-E0R" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="83" id="gTK-jF-eL0"/>
130+
<constraint firstItem="vCS-SU-E0R" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="80" id="gTK-jF-eL0"/>
68131
<constraint firstAttribute="centerX" secondItem="Pbc-5y-GLg" secondAttribute="centerX" id="mBu-C3-nZu"/>
69132
<constraint firstAttribute="centerX" secondItem="vCS-SU-E0R" secondAttribute="centerX" id="tYU-co-tHf"/>
70133
</constraints>
@@ -75,4 +138,7 @@
75138
<point key="canvasLocation" x="255" y="225"/>
76139
</scene>
77140
</scenes>
141+
<resources>
142+
<image name="screen.jpg" width="600" height="880"/>
143+
</resources>
78144
</document>

TYAlertControllerDemo/TYAlertController/TYAlertController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ - (void)dealloc
277277
{
278278
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
279279
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
280-
NSLog(@"TYAlertController dealloc");
280+
NSLog(@"%@ dealloc",NSStringFromClass([self class]));
281281
}
282282

283283
@end

TYAlertControllerDemo/TYAlertController/TYAlertView.m

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,14 @@ - (void)addTextLabels
196196
_messageLabel = messageLabel;
197197
}
198198

199+
- (void)didMoveToSuperview
200+
{
201+
if (self.superview) {
202+
[self layoutContentViews];
203+
[self layoutTextLabels];
204+
}
205+
}
206+
199207
- (void)addAction:(TYAlertAction *)action
200208
{
201209
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
@@ -213,11 +221,6 @@ - (void)addAction:(TYAlertAction *)action
213221
[_buttons addObject:button];
214222
[_actions addObject:action];
215223

216-
if (_buttons.count == 1) {
217-
[self layoutContentViews];
218-
[self layoutTextLabels];
219-
}
220-
221224
[self layoutButtons];
222225
}
223226

@@ -257,6 +260,9 @@ - (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textFeild))c
257260

258261
- (void)layoutContentViews
259262
{
263+
if (!_textContentView.translatesAutoresizingMaskIntoConstraints) {
264+
return;
265+
}
260266
// textContentView
261267
_textContentView.translatesAutoresizingMaskIntoConstraints = NO;
262268

@@ -278,6 +284,9 @@ - (void)layoutContentViews
278284

279285
- (void)layoutTextLabels
280286
{
287+
if (!_titleLable.translatesAutoresizingMaskIntoConstraints && !_messageLabel.translatesAutoresizingMaskIntoConstraints) {
288+
return;
289+
}
281290
// title
282291
_titleLable.translatesAutoresizingMaskIntoConstraints = NO;
283292
[_textContentView addConstarintWithView:_titleLable topView:_textContentView leftView:_textContentView bottomView:nil rightView:_textContentView edageInset:UIEdgeInsetsZero];
@@ -292,9 +301,6 @@ - (void)layoutButtons
292301
{
293302
UIButton *button = _buttons.lastObject;
294303
if (_buttons.count == 1) {
295-
296-
297-
298304
[_buttonContentView addConstraintToView:button edageInset:UIEdgeInsetsZero];
299305
[button addConstarintWidth:0 height:_buttonHeight];
300306
}else if (_buttons.count == 2) {
@@ -366,12 +372,9 @@ - (void)actionButtonClicked:(UIButton *)button
366372
[self.viewController dismissViewControllerAnimated:YES completion:nil];
367373
}
368374

369-
/*
370-
// Only override drawRect: if you perform custom drawing.
371-
// An empty implementation adversely affects performance during animation.
372-
- (void)drawRect:(CGRect)rect {
373-
// Drawing code
374-
}
375-
*/
375+
- (void)dealloc
376+
{
377+
NSLog(@"%@ dealloc",NSStringFromClass([self class]));
378+
}
376379

377380
@end

TYAlertControllerDemo/TYAlertController/TYShowAlertView.m

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,17 @@ - (void)setBackgoundTapDismissEnable:(BOOL)backgoundTapDismissEnable
105105
_singleTap.enabled = backgoundTapDismissEnable;
106106
}
107107

108+
- (void)didMoveToSuperview
109+
{
110+
if (self.superview) {
111+
self.translatesAutoresizingMaskIntoConstraints = NO;
112+
[self.superview addConstraintToView:self edageInset:UIEdgeInsetsZero];
113+
[self layoutAlertView];
114+
}
115+
}
116+
108117
- (void)layoutAlertView
109118
{
110-
//[self layoutIfNeeded];
111119
_alertView.translatesAutoresizingMaskIntoConstraints = NO;
112120
// center X
113121
[self addConstraintCenterXToView:_alertView CenterYToView:nil];
@@ -126,7 +134,7 @@ - (void)layoutAlertView
126134
}
127135

128136
if (!findAlertViewWidthConstraint) {
129-
[_alertView addConstarintWidth:CGRectGetWidth(kCurrentWindow.frame)-2*_alertViewEdging height:0];
137+
[_alertView addConstarintWidth:CGRectGetWidth(self.superview.frame)-2*_alertViewEdging height:0];
130138
}
131139
}
132140

@@ -135,7 +143,7 @@ - (void)layoutAlertView
135143

136144
if (_alertViewOriginY > 0) {
137145
[_alertView layoutIfNeeded];
138-
alertViewCenterYConstraint.constant = _alertViewOriginY - (CGRectGetHeight(kCurrentWindow.frame) - CGRectGetHeight(_alertView.frame))/2;
146+
alertViewCenterYConstraint.constant = _alertViewOriginY - (CGRectGetHeight(self.superview.frame) - CGRectGetHeight(_alertView.frame))/2;
139147
}
140148
}
141149

@@ -161,9 +169,6 @@ - (void)show
161169
{
162170
if (self.superview == nil) {
163171
[kCurrentWindow addSubview:self];
164-
self.translatesAutoresizingMaskIntoConstraints = NO;
165-
[kCurrentWindow addConstraintToView:self edageInset:UIEdgeInsetsZero];
166-
[self layoutAlertView];
167172
}
168173
self.alpha = 0;
169174
_alertView.transform = CGAffineTransformScale(_alertView.transform,0.1,0.1);
@@ -186,12 +191,9 @@ - (void)hide
186191
}
187192
}
188193

189-
/*
190-
// Only override drawRect: if you perform custom drawing.
191-
// An empty implementation adversely affects performance during animation.
192-
- (void)drawRect:(CGRect)rect {
193-
// Drawing code
194+
- (void)dealloc
195+
{
196+
NSLog(@"%@ dealloc",NSStringFromClass([self class]));
194197
}
195-
*/
196198

197199
@end

TYAlertControllerDemo/UIImage+ImageEffects.m

Lines changed: 1 addition & 1 deletion
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:30 tintColor:tintColor saturationDeltaFactor:1.8 maskImage:nil];
108+
return [self applyBlurWithRadius:20 tintColor:tintColor saturationDeltaFactor:1.8 maskImage:nil];
109109
}
110110

111111

0 commit comments

Comments
 (0)