Simulator Screen Shot - iPhone 11 Pro - 2020-09-25 at 18.12.40.png
OC代碼,主要處理彈出各種view
.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
typedef enum : NSUInteger {
PopDirectionTop,
PopDirectionLeft,
PopDirectionBottom,
PopDirectionRight,
PopDirectionCenter
} PopDirection;
@interface PopupTool : NSObject
/**從不同方向彈出自定義視圖
給UIView增加了個自定義屬性popSuccessBlock,popDismissBlock,這個單獨在各自的popview的點擊消失事件中調(diào)用
*/
+(void)pop:(UIView *)popView TargetView:(UIView *)targetView PopDirection:(PopDirection)popDirection;
@end
NS_ASSUME_NONNULL_END
.m
#import "PopupTool.h"
@implementation PopupTool
+(void)pop:(UIView *)popView TargetView:(UIView *)targetView PopDirection:(PopDirection)popDirection{
//蒙版背景
UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, targetView.width, targetView.height)];
[targetView addSubview:backView];
[PopupTool getPopViewFromFramWith:popView PopDirection:popDirection];
if (![targetView.subviews containsObject:popView]) {
if (popView.width == 0 || popView.height == 0) {
popView.size = CGSizeMake(WIDTH, HEIGHT);
}
[targetView addSubview:popView];
}
//彈性動畫
[UIView animateWithDuration:0.5 delay:0.0 usingSpringWithDamping:0.9 initialSpringVelocity:5.0 options:(UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction) animations:^{
[PopupTool getPopViewToFramWith:popView PopDirection:popDirection];
} completion:^(BOOL finished) {
[PopupTool getPopViewToFramWith:popView PopDirection:popDirection];
BLOCK_EXEC(popView.popSuccessBlock);
}];
[PopupTool animateChangeBackgroundColorDarkWith:backView Duration:0.5];
//處理消失事件(給UIView增加了個自定義屬性popDismissBlock,這個單獨在各自的popview的點擊消失事件中調(diào)用)
__weak typeof(popView) weakPopView = popView;
void (^dismiss)(void) = ^{
[PopupTool animateChangeBackgroundColorLightWith:backView Duration:0.5];
[UIView animateWithDuration:0.5 delay:0.0 usingSpringWithDamping:0.9 initialSpringVelocity:5.0 options:(UIViewAnimationOptionCurveEaseInOut) animations:^{
[PopupTool getPopViewFromFramWith:weakPopView PopDirection:popDirection];
} completion:^(BOOL finished) {
[weakPopView removeFromSuperview];
[backView removeFromSuperview];
}];
};
popView.popDismissBlock = dismiss;
}
//TODO: -- 私有事件
+(void)getPopViewFromFramWith:(UIView *)popView PopDirection:(PopDirection)popDirection{
if (popDirection == PopDirectionTop) {
popView.x = 0;
popView.y = -HEIGHT;
}else if (popDirection == PopDirectionLeft) {
popView.x = -WIDTH;
popView.y = 0;
}else if (popDirection == PopDirectionBottom) {
popView.x = 0;
popView.y = HEIGHT;
}else if (popDirection == PopDirectionRight) {
popView.x = WIDTH;
popView.y = 0;
}else if (popDirection == PopDirectionCenter) {
popView.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
popView.transform = CGAffineTransformMakeScale(0.0001, 0.0001);
}
}
+(void)getPopViewToFramWith:(UIView *)popView PopDirection:(PopDirection)popDirection{
if (popDirection == PopDirectionCenter) {
popView.transform = CGAffineTransformMakeScale(1.0, 1.0);
}else{
popView.x = 0;
popView.y = 0;
}
}
//背景色逐步加深
+(void)animateChangeBackgroundColorDarkWith:(UIView *)view Duration:(NSTimeInterval)duration{
[UIView animateKeyframesWithDuration:duration delay:0.f options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
[UIView addKeyframeWithRelativeStartTime:0.f relativeDuration:1.0 / 4 animations:^{
view.backgroundColor = RGBAColor(0, 0, 0, 0.2);
}];
[UIView addKeyframeWithRelativeStartTime:1.0 / 4 relativeDuration:1.0 / 4 animations:^{
view.backgroundColor = RGBAColor(0, 0, 0, 0.4);
}];
[UIView addKeyframeWithRelativeStartTime:2.0 / 4 relativeDuration:1.0 / 4 animations:^{
view.backgroundColor = RGBAColor(0, 0, 0, 0.6);
}];
[UIView addKeyframeWithRelativeStartTime:3.0 / 4 relativeDuration:1.0 / 4 animations:^{
view.backgroundColor = RGBAColor(0, 0, 0, 0.8);
}];
} completion:^(BOOL finished) {
NSLog(@"動畫結(jié)束");
}];
}
//背景色逐步變淺
+(void)animateChangeBackgroundColorLightWith:(UIView *)view Duration:(NSTimeInterval)duration{
[UIView animateKeyframesWithDuration:duration delay:0.f options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
[UIView addKeyframeWithRelativeStartTime:0.f relativeDuration:1.0 / 4 animations:^{
view.backgroundColor = RGBAColor(0, 0, 0, 0.6);
}];
[UIView addKeyframeWithRelativeStartTime:1.0 / 4 relativeDuration:1.0 / 4 animations:^{
view.backgroundColor = RGBAColor(0, 0, 0, 0.4);
}];
[UIView addKeyframeWithRelativeStartTime:2.0 / 4 relativeDuration:1.0 / 4 animations:^{
view.backgroundColor = RGBAColor(0, 0, 0, 0.2);
}];
[UIView addKeyframeWithRelativeStartTime:3.0 / 4 relativeDuration:1.0 / 4 animations:^{
view.backgroundColor = RGBAColor(0, 0, 0, 0.0);
}];
} completion:^(BOOL finished) {
NSLog(@"動畫結(jié)束");
}];
}
@end
給UIView增加category
.h
/**pop視圖單獨使用*/
@property (nonatomic, copy) void(^popSuccessBlock)(void);
@property (nonatomic, copy) void(^popDismissBlock)(void);
.m
-(void)setPopSuccessBlock:(void (^)(void))popSuccessBlock{
objc_setAssociatedObject(self, @"popSuccessBlock", popSuccessBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
-(void (^)(void))popSuccessBlock{
return objc_getAssociatedObject(self, @"popSuccessBlock");
}
-(void)setPopDismissBlock:(void (^)(void))popDismissBlock{
objc_setAssociatedObject(self, @"popDismissBlock", popDismissBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
-(void (^)(void))popDismissBlock{
return objc_getAssociatedObject(self, @"popDismissBlock");
}
具體使用:
//具體在那個view層上彈出可自定義
OptionOrderSureView *view = [[OptionOrderSureView alloc] init];
[PopupTool pop:view TargetView:KeyWindow PopDirection:(PopDirectionCenter)];
//OptionOrderSureView 占用整個window即可, 彈窗視圖的close事件調(diào)用下
-(void)close{
BLOCK_EXEC(self.popDismissBlock);
}