Skip to content

Commit fade0aa

Browse files
12207480tanyang
authored andcommitted
add showAlertView and uiview category
1 parent 16deb20 commit fade0aa

16 files changed

+452
-58
lines changed

TYAlertControllerDemo.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
B0873F0D1BD370C700D2F4D5 /* TYShowAlertView.m in Sources */ = {isa = PBXBuildFile; fileRef = B0873F0C1BD370C700D2F4D5 /* TYShowAlertView.m */; settings = {ASSET_TAGS = (); }; };
1011
D35854241B9532D700B5FA1F /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = D35854231B9532D700B5FA1F /* main.m */; };
1112
D35854271B9532D700B5FA1F /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = D35854261B9532D700B5FA1F /* AppDelegate.m */; };
1213
D358542A1B9532D700B5FA1F /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = D35854291B9532D700B5FA1F /* ViewController.m */; };
@@ -35,6 +36,8 @@
3536
/* End PBXContainerItemProxy section */
3637

3738
/* Begin PBXFileReference section */
39+
B0873F0B1BD370C700D2F4D5 /* TYShowAlertView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TYShowAlertView.h; sourceTree = "<group>"; };
40+
B0873F0C1BD370C700D2F4D5 /* TYShowAlertView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TYShowAlertView.m; sourceTree = "<group>"; };
3841
D358541E1B9532D700B5FA1F /* TYAlertControllerDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TYAlertControllerDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
3942
D35854221B9532D700B5FA1F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4043
D35854231B9532D700B5FA1F /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
@@ -151,6 +154,8 @@
151154
D35854A41B953FC300B5FA1F /* TYAlertController.h */,
152155
D35854A51B953FC300B5FA1F /* TYAlertController.m */,
153156
D35854B71B95BC9700B5FA1F /* TYAlertController+TransitionAnimate.m */,
157+
B0873F0B1BD370C700D2F4D5 /* TYShowAlertView.h */,
158+
B0873F0C1BD370C700D2F4D5 /* TYShowAlertView.m */,
154159
);
155160
path = TYAlertController;
156161
sourceTree = "<group>";
@@ -288,6 +293,7 @@
288293
D35854271B9532D700B5FA1F /* AppDelegate.m in Sources */,
289294
D3C1A9731B9D16D5009738B0 /* UIView+TYAlertView.m in Sources */,
290295
D395FE1F1B96C7F1000F72F7 /* TYAlertScaleFadeAnimation.m in Sources */,
296+
B0873F0D1BD370C700D2F4D5 /* TYShowAlertView.m in Sources */,
291297
D35854241B9532D700B5FA1F /* main.m in Sources */,
292298
D35854A61B953FC300B5FA1F /* TYAlertController.m in Sources */,
293299
D35854B81B95BC9700B5FA1F /* TYAlertController+TransitionAnimate.m in Sources */,

TYAlertControllerDemo/AppDelegate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// AppDelegate.h
33
// TYAlertControllerDemo
44
//
5-
// Created by SunYong on 15/9/1.
5+
// Created by tanyang on 15/9/1.
66
// Copyright (c) 2015年 tanyang. All rights reserved.
77
//
88

TYAlertControllerDemo/AppDelegate.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// AppDelegate.m
33
// TYAlertControllerDemo
44
//
5-
// Created by SunYong on 15/9/1.
5+
// Created by tanyang on 15/9/1.
66
// Copyright (c) 2015年 tanyang. All rights reserved.
77
//
88

TYAlertControllerDemo/Base.lproj/Main.storyboard

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,25 @@
4545
<action selector="showActionSheet:" destination="BYZ-38-t0r" eventType="touchUpInside" id="6ok-Ne-kuo"/>
4646
</connections>
4747
</button>
48+
<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"/>
50+
<animations/>
51+
<constraints>
52+
<constraint firstAttribute="height" constant="30" id="U3E-o2-zSc"/>
53+
<constraint firstAttribute="width" constant="139" id="eKz-vX-AMM"/>
54+
</constraints>
55+
<state key="normal" title="AlertViewInWindow"/>
56+
<connections>
57+
<action selector="showAlertViewInWindow:" destination="BYZ-38-t0r" eventType="touchUpInside" id="PKv-Os-aIC"/>
58+
</connections>
59+
</button>
4860
</subviews>
4961
<animations/>
5062
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
5163
<constraints>
64+
<constraint firstItem="1WY-V3-iTv" firstAttribute="top" secondItem="Pbc-5y-GLg" secondAttribute="bottom" constant="42" id="437-WF-XhU"/>
5265
<constraint firstItem="Pbc-5y-GLg" firstAttribute="top" secondItem="vCS-SU-E0R" secondAttribute="bottom" constant="35" id="XMM-Dk-17g"/>
66+
<constraint firstItem="1WY-V3-iTv" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="g2R-k6-tZu"/>
5367
<constraint firstItem="vCS-SU-E0R" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="83" id="gTK-jF-eL0"/>
5468
<constraint firstAttribute="centerX" secondItem="Pbc-5y-GLg" secondAttribute="centerX" id="mBu-C3-nZu"/>
5569
<constraint firstAttribute="centerX" secondItem="vCS-SU-E0R" secondAttribute="centerX" id="tYU-co-tHf"/>
@@ -58,6 +72,7 @@
5872
</viewController>
5973
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
6074
</objects>
75+
<point key="canvasLocation" x="255" y="225"/>
6176
</scene>
6277
</scenes>
6378
</document>

TYAlertControllerDemo/TYAlertController/TYAlertController+TransitionAnimate.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// TYAlertController+TransitionAnimate.m
33
// TYAlertControllerDemo
44
//
5-
// Created by SunYong on 15/9/1.
5+
// Created by tanyang on 15/9/1.
66
// Copyright (c) 2015年 tanyang. All rights reserved.
77
//
88

TYAlertControllerDemo/TYAlertController/TYAlertController.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
// TYAlertController.h
33
// TYAlertControllerDemo
44
//
5-
// Created by SunYong on 15/9/1.
5+
// Created by tanyang on 15/9/1.
66
// Copyright (c) 2015年 tanyang. All rights reserved.
77
//
88

99
#import <UIKit/UIKit.h>
10+
#import "TYAlertView.h"
1011

1112
typedef NS_ENUM(NSInteger, TYAlertControllerStyle) {
1213
TYAlertControllerStyleAlert = 0,
@@ -32,21 +33,24 @@ typedef NS_ENUM(NSInteger, TYAlertTransitionAnimation) {
3233

3334
@property (nonatomic, assign, readonly) Class transitionAnimationClass;
3435

35-
@property (nonatomic, assign) BOOL backgoundTapEnable; // default YES
36+
@property (nonatomic, assign) BOOL backgoundTapDismissEnable; // default YES
3637

3738
@property (nonatomic, assign) CGFloat alertViewOriginY; // default center Y
3839

3940
@property (nonatomic, assign) CGFloat alertViewEdging; // only alertStyle, when width frame equal to 0,or no width constraint ,this proprty will use, default to 15
4041

4142
@property (nonatomic, copy) void (^dismissComplete)(void); // dismiss controller completed block
4243

44+
+ (instancetype)alertControllerWithAlertView:(UIView *)alertView;
4345

4446
+ (instancetype)alertControllerWithAlertView:(UIView *)alertView preferredStyle:(TYAlertControllerStyle)preferredStyle;
4547

4648
+ (instancetype)alertControllerWithAlertView:(UIView *)alertView preferredStyle:(TYAlertControllerStyle)preferredStyle transitionAnimation:(TYAlertTransitionAnimation)transitionAnimation;
4749

4850
+ (instancetype)alertControllerWithAlertView:(UIView *)alertView preferredStyle:(TYAlertControllerStyle)preferredStyle transitionAnimationClass:(Class)transitionAnimationClass;
4951

52+
- (void)dismissViewControllerAnimated: (BOOL)animated;
53+
5054
@end
5155

5256
// Transition Animate

TYAlertControllerDemo/TYAlertController/TYAlertController.m

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// TYAlertController.m
33
// TYAlertControllerDemo
44
//
5-
// Created by SunYong on 15/9/1.
5+
// Created by tanyang on 15/9/1.
66
// Copyright (c) 2015年 tanyang. All rights reserved.
77
//
88

@@ -19,6 +19,8 @@ @interface TYAlertController ()
1919

2020
@property (nonatomic, assign) Class transitionAnimationClass;
2121

22+
@property (nonatomic, weak) UITapGestureRecognizer *singleTap;
23+
2224
@end
2325

2426
@implementation TYAlertController
@@ -60,19 +62,36 @@ - (instancetype)initWithAlertView:(UIView *)alertView preferredStyle:(TYAlertCon
6062
return self;
6163
}
6264

65+
+ (instancetype)alertControllerWithAlertView:(UIView *)alertView
66+
{
67+
return [[self alloc]initWithAlertView:alertView
68+
preferredStyle:TYAlertControllerStyleAlert
69+
transitionAnimation:TYAlertTransitionAnimationFade
70+
transitionAnimationClass:nil];
71+
}
72+
6373
+ (instancetype)alertControllerWithAlertView:(UIView *)alertView preferredStyle:(TYAlertControllerStyle)preferredStyle
6474
{
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];
6679
}
6780

6881
+ (instancetype)alertControllerWithAlertView:(UIView *)alertView preferredStyle:(TYAlertControllerStyle)preferredStyle transitionAnimation:(TYAlertTransitionAnimation)transitionAnimation
6982
{
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];
7187
}
7288

7389
+ (instancetype)alertControllerWithAlertView:(UIView *)alertView preferredStyle:(TYAlertControllerStyle)preferredStyle transitionAnimationClass:(Class)transitionAnimationClass
7490
{
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];
7695
}
7796

7897
#pragma mark - life cycle
@@ -81,6 +100,7 @@ - (void)viewDidLoad {
81100
[super viewDidLoad];
82101
// Do any additional setup after loading the view.
83102
self.view.backgroundColor = [UIColor clearColor];
103+
84104
[self addBackgroundView];
85105

86106
[self addSingleTapGesture];
@@ -91,36 +111,44 @@ - (void)viewDidLoad {
91111

92112
}
93113

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-
103114
- (void)addBackgroundView
104115
{
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];
107122
}
108123

109124
- (void)addSingleTapGesture
110125
{
111-
if (!self.backgoundTapEnable) {
112-
return;
113-
}
114-
// 点击删除
115126
self.view.userInteractionEnabled = YES;
116-
//单指单击
127+
117128
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+
123131
[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;
124152
}
125153

126154
- (void)configureController
@@ -129,7 +157,7 @@ - (void)configureController
129157
self.definesPresentationContext = YES;
130158
self.modalPresentationStyle = UIModalPresentationCustom;
131159
self.transitioningDelegate = self;
132-
_backgoundTapEnable = YES;
160+
_backgoundTapDismissEnable = YES;
133161
_alertViewEdging = 15;
134162
}
135163

@@ -163,7 +191,6 @@ - (void)layoutAlertStyleView
163191

164192
// top Y
165193
if (_alertViewOriginY > 0) {
166-
167194
[self.view addConstarintWithView:_alertView topView:self.view leftView:nil bottomView:nil rightView:nil edageInset:UIEdgeInsetsMake(_alertViewOriginY, 0, 0, 0)];
168195
}else {
169196
[self.view addConstraintCenterXToView:nil CenterYToView:_alertView];
@@ -202,11 +229,16 @@ - (void)layoutActionSheetStyleView
202229

203230
}
204231

232+
- (void)dismissViewControllerAnimated:(BOOL)animated
233+
{
234+
[self dismissViewControllerAnimated:YES completion:self.dismissComplete];
235+
}
236+
205237
#pragma mark - action
206238

207239
- (void)singleTap:(UITapGestureRecognizer *)sender
208240
{
209-
[self dismissViewControllerAnimated:YES completion:self.dismissComplete];
241+
[self dismissViewControllerAnimated:YES];
210242
}
211243

212244
- (void)didReceiveMemoryWarning {

TYAlertControllerDemo/TYAlertController/TYAlertView.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22
// TYAlertView.h
33
// TYAlertControllerDemo
44
//
5-
// Created by SunYong on 15/9/7.
5+
// Created by tanyang on 15/9/7.
66
// Copyright (c) 2015年 tanyang. All rights reserved.
77
//
88

9-
#import "UIView+TYAlertView.h"
9+
#import <UIKit/UIKit.h>
1010

11-
typedef enum : NSUInteger {
11+
typedef NS_ENUM(NSUInteger, TYAlertActionStyle) {
1212
TYAlertActionStyleDefault,
1313
TYAlertActionStyleCancle,
1414
TYAlertActionStyleDestructive,
15-
} TYAlertActionStyle;
16-
15+
};
1716

1817
@interface TYAlertAction : NSObject <NSCopying>
1918

@@ -42,6 +41,11 @@ typedef enum : NSUInteger {
4241
@property (nonatomic, assign) CGFloat buttonHeight;
4342
@property (nonatomic, assign) CGFloat buttonSpace;
4443
@property (nonatomic, assign) CGFloat buttonContentViewEdge;
44+
@property (nonatomic, assign) CGFloat buttonCornerRadius;
45+
@property (nonatomic, strong) UIFont *buttonFont;
46+
@property (nonatomic, strong) UIColor *buttonDefaultBgColor;
47+
@property (nonatomic, strong) UIColor *buttonCancleBgColor;
48+
@property (nonatomic, strong) UIColor *buttonDestructiveBgColor;
4549

4650
// textFeild custom
4751
@property (nonatomic, strong) UIColor *textFieldBorderColor;

0 commit comments

Comments
 (0)