Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

改进alertView show位置的策略 #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion TYAlertControllerDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@
TargetAttributes = {
D358541D1B9532D700B5FA1F = {
CreatedOnToolsVersion = 6.4;
DevelopmentTeam = ZW52Q3KXX4;
DevelopmentTeam = YVL7699HA5;
};
D35854361B9532D700B5FA1F = {
CreatedOnToolsVersion = 6.4;
Expand All @@ -298,6 +298,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
Base,
);
Expand Down Expand Up @@ -488,6 +489,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEVELOPMENT_TEAM = YVL7699HA5;
INFOPLIST_FILE = TYAlertControllerDemo/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array/>
</plist>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>TYAlertControllerDemo.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>TYAlertControllerDemo.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>
32 changes: 28 additions & 4 deletions TYAlertControllerDemo/TYAlertController/TYShowAlertView.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,40 @@
@property (nonatomic, strong) UIView *backgroundView;

@property (nonatomic, assign) BOOL backgoundTapDismissEnable; // default NO
@property (nonatomic, assign) CGFloat alertViewOriginY; // default center Y
@property (nonatomic, assign) CGFloat alertViewEdging; // default 15


/** 2019-10-04 12:13:56
取值优先级为 ( 数字越小, 优先级越高 )
1. NSLayoutAttributeWidth, 如果有宽度layout,则以layout为准
2. 主动赋值了alertViewWidth,则以_alertViewWidth为准
3. alertView.size != {0 , 0} , 则以 alertView.size 为准
4. superView.width - 2 * 15
*/
@property (nonatomic, assign) CGFloat alertViewWidth;

/** 2019-10-04 12:22:41
取值优先级为 ( 数字越小, 优先级越高 )
1. NSLayoutAttributeHeight, 如果有宽度layout,则以layout为准
2. 主动赋值了alertViewHeight属性,则以_alertViewHeight为准
3. alertView.size != {0 , 0} , 则以 alertView.size 为准
4. 200
*/
@property (nonatomic, assign) CGFloat alertViewHeight;

/** 2019-10-04 12:24:03
1. 主动赋值了alertViewCenter,则以_alertViewCenter为准
2. alertView.size != {0 , 0}, 则视为想要使用frame进行约束,赋值CGPointMake(CGRectGetMidX(_alertView.frame), CGRectGetMidY(_alertView.frame))
3.kCurrentWindow.center
*/
@property (nonatomic, assign) CGPoint alertViewCenter;

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

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

+(void)showAlertViewWithView:(UIView *)alertView originY:(CGFloat)originY;
+(void)showAlertViewWithView:(UIView *)alertView center:(CGPoint)center;

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

+ (instancetype)alertViewWithView:(UIView *)alertView;

Expand Down
73 changes: 45 additions & 28 deletions TYAlertControllerDemo/TYAlertController/TYShowAlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ - (instancetype)initWithFrame:(CGRect)frame
self.backgroundColor = [UIColor clearColor];

_backgoundTapDismissEnable = NO;
_alertViewEdging = 15;

[self addBackgroundView];

Expand Down Expand Up @@ -61,16 +60,16 @@ + (void)showAlertViewWithView:(UIView *)alertView backgoundTapDismissEnable:(BOO
[showTipView show];
}

+ (void)showAlertViewWithView:(UIView *)alertView originY:(CGFloat)originY
+ (void)showAlertViewWithView:(UIView *)alertView center:(CGPoint)center
{
[self showAlertViewWithView:alertView
originY:originY backgoundTapDismissEnable:NO];
center:center backgoundTapDismissEnable:NO];
}

+ (void)showAlertViewWithView:(UIView *)alertView originY:(CGFloat)originY backgoundTapDismissEnable:(BOOL)backgoundTapDismissEnable
+ (void)showAlertViewWithView:(UIView *)alertView center:(CGPoint)center backgoundTapDismissEnable:(BOOL)backgoundTapDismissEnable
{
TYShowAlertView *showTipView = [self alertViewWithView:alertView];
showTipView.alertViewOriginY = originY;
showTipView.alertViewCenter = center;
showTipView.backgoundTapDismissEnable = backgoundTapDismissEnable;
[showTipView show];
}
Expand Down Expand Up @@ -114,34 +113,51 @@ - (void)didMoveToSuperview
- (void)layoutAlertView
{
_alertView.translatesAutoresizingMaskIntoConstraints = NO;
// center X
[self addConstraintCenterXToView:_alertView centerYToView:nil];

// width, height
if (!CGSizeEqualToSize(_alertView.frame.size,CGSizeZero)) {
[_alertView addConstraintWidth:CGRectGetWidth(_alertView.frame) height:CGRectGetHeight(_alertView.frame)];

}else {
BOOL findAlertViewWidthConstraint = NO;
for (NSLayoutConstraint *constraint in _alertView.constraints) {
if (constraint.firstAttribute == NSLayoutAttributeWidth) {
findAlertViewWidthConstraint = YES;
break;
}
}

if (!findAlertViewWidthConstraint) {
[_alertView addConstraintWidth:CGRectGetWidth(self.superview.frame)-2*_alertViewEdging height:0];
}
BOOL findAlertViewWidthConstraint = NO;
BOOL findAlertViewHeightConstraint = NO;
BOOL hasSetFrame = !CGSizeEqualToSize(_alertView.frame.size,CGSizeZero);

/**2019-10-04 12:10:15 如果没有人为设置_alertViewCenter, 则取frame的对应的center*/
if (CGPointEqualToPoint(_alertViewCenter, CGPointZero) && hasSetFrame) {
_alertViewCenter = CGPointMake(CGRectGetMidX(_alertView.frame), CGRectGetMidY(_alertView.frame));
}

// topY
NSLayoutConstraint *alertViewCenterYConstraint = [self addConstraintCenterYToView:_alertView constant:0];
/**2019-10-04 12:11:52 alertViewCenter*/
[self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_alertView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:-_alertViewCenter.x]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual
toItem:_alertView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:-_alertViewCenter.y]];

/**2019-10-04 13:17:47 alertViewWidth & _alertViewHeight*/
for (NSLayoutConstraint *constraint in _alertView.constraints) {
if (constraint.firstAttribute == NSLayoutAttributeWidth) {
findAlertViewWidthConstraint = YES;
_alertViewWidth = constraint.constant;
}else if (constraint.firstAttribute == NSLayoutAttributeHeight){
findAlertViewHeightConstraint = YES;
_alertViewHeight = constraint.constant;
}
}

if (_alertViewOriginY > 0) {
[_alertView layoutIfNeeded];
alertViewCenterYConstraint.constant = _alertViewOriginY - (CGRectGetHeight(self.superview.frame) - CGRectGetHeight(_alertView.frame))/2;
if (!findAlertViewWidthConstraint && !_alertViewWidth) {
/**2019-10-04 12:10:15 如果没有人为设置_alertViewWidth, 则从frame中取*/
if (hasSetFrame) {
_alertViewWidth = _alertView.frame.size.width;
}else{
_alertViewWidth = CGRectGetWidth(self.superview.frame)-2*15;
}
[_alertView addConstraintWidth:_alertViewWidth height:0];
}

if (!findAlertViewHeightConstraint && !_alertViewHeight) {
/**2019-10-04 12:10:15 如果没有人为设置_alertViewHeight, default:200*/
if (hasSetFrame) {
_alertViewHeight = _alertView.frame.size.height;
}else{
_alertViewWidth = 200;
}
[_alertView addConstraintWidth:0 height:_alertViewHeight];
}
}

#pragma mark - add Gesture
Expand Down Expand Up @@ -183,6 +199,7 @@ - (void)hide
_alertView.transform = CGAffineTransformScale(_alertView.transform,0.1,0.1);
self.alpha = 0;
} completion:^(BOOL finished) {
_alertView.transform = CGAffineTransformIdentity;
[self removeFromSuperview];
}];
}
Expand Down
4 changes: 2 additions & 2 deletions TYAlertControllerDemo/TYAlertController/UIView+TYAlertView.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
// backgoundTapDismissEnable default NO
- (void)showInWindowWithBackgoundTapDismissEnable:(BOOL)backgoundTapDismissEnable;

- (void)showInWindowWithOriginY:(CGFloat)OriginY;
- (void)showInWindowWithCenter:(CGPoint)center;

- (void)showInWindowWithOriginY:(CGFloat)OriginY backgoundTapDismissEnable:(BOOL)backgoundTapDismissEnable;
- (void)showInWindowWithCenter:(CGPoint)center backgoundTapDismissEnable:(BOOL)backgoundTapDismissEnable;


#pragma mark - hide
Expand Down
8 changes: 4 additions & 4 deletions TYAlertControllerDemo/TYAlertController/UIView+TYAlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ - (void)showInWindowWithBackgoundTapDismissEnable:(BOOL)backgoundTapDismissEnabl
[TYShowAlertView showAlertViewWithView:self backgoundTapDismissEnable:backgoundTapDismissEnable];
}

- (void)showInWindowWithOriginY:(CGFloat)OriginY
- (void)showInWindowWithCenter:(CGPoint)center
{
[self showInWindowWithOriginY:OriginY backgoundTapDismissEnable:NO];
[self showInWindowWithCenter:center backgoundTapDismissEnable:NO];
}

- (void)showInWindowWithOriginY:(CGFloat)OriginY backgoundTapDismissEnable:(BOOL)backgoundTapDismissEnable
- (void)showInWindowWithCenter:(CGPoint)center backgoundTapDismissEnable:(BOOL)backgoundTapDismissEnable
{
if (self.superview) {
[self removeFromSuperview];
}
[TYShowAlertView showAlertViewWithView:self originY:OriginY backgoundTapDismissEnable:backgoundTapDismissEnable];
[TYShowAlertView showAlertViewWithView:self center:center backgoundTapDismissEnable:backgoundTapDismissEnable];
}

- (void)hideInWindow
Expand Down
8 changes: 6 additions & 2 deletions TYAlertControllerDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#import "SettingModelView.h"
#import "ShareView.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kCurrentWindowCenter [UIApplication sharedApplication].keyWindow.center

@interface ViewController ()
@end

Expand Down Expand Up @@ -158,7 +162,7 @@ - (IBAction)showAlertViewInWindowAction:(id)sender {
}]];

// first way to show ,use UIView Category
[alertView showInWindowWithOriginY:200 backgoundTapDismissEnable:YES];
[alertView showInWindowWithCenter:kCurrentWindowCenter backgoundTapDismissEnable:YES];

// second way to show
//[TYShowAlertView showAlertViewWithView:alertView originY:200 backgoundTapDismissEnable:YES];
Expand All @@ -168,7 +172,7 @@ - (IBAction)customViewInWindowAction:(id)sender {
ShareView *shareView = [ShareView createViewFromNib];

// use UIView Category
[shareView showInWindow];
[shareView showInWindowWithCenter:CGPointMake(kScreenWidth / 2.0, 380)];
}

- (void)didReceiveMemoryWarning {
Expand Down