美文网首页
弹出视图的动画工具类

弹出视图的动画工具类

作者: 辛乐 | 来源:发表于2020-09-25 18:30 被阅读0次
    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的点击消失事件中调用
     */
    +(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的点击消失事件中调用)
       __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(@"动画结束");
        }];
    }
    
    //背景色逐步变浅
    +(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(@"动画结束");
        }];
    }
    
    @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事件调用下
    -(void)close{
        BLOCK_EXEC(self.popDismissBlock);
    }
    

    相关文章

      网友评论

          本文标题:弹出视图的动画工具类

          本文链接:https://www.haomeiwen.com/subject/mwnfuktx.html