View 动画

作者: 失忆的程序员 | 来源:发表于2021-08-09 09:40 被阅读0次
    
    #import <Foundation/Foundation.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface XAnimation : NSObject
    
    /// 抖动 Shake
    + (void)x_AnimaionShakeView:(UIView *)view repeatCount:(NSInteger)repeatCounts;
    /// 缩放
    + (void)x_AnimaionCircleRunView:(UIView *)View;
    /// 不使用时记得移除动画
    + (void)x_StopView:(UIView *)View;
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    
    
    
    #import "XAnimation.h"
    
    @implementation XAnimation
    
    
    #pragma mark ----- 缩放
    + (void)x_AnimaionCircleRunView:(UIView *)View
    {
        CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        scaleAnimation.duration = 1;
        //scaleAnimation.repeatCount = HUGE_VALF;
        scaleAnimation.repeatCount = 1;//动画次数
        scaleAnimation.autoreverses = YES;
        //removedOnCompletion为NO保证app切换到后台动画再切回来时动画依然执行
        scaleAnimation.removedOnCompletion = NO;
        scaleAnimation.fromValue = @(0.9);
        scaleAnimation.toValue = @(1.0);
        [View.layer addAnimation:scaleAnimation forKey:@"scale-layer"];
    }
    
    /// 不使用时记得移除动画
    + (void)x_StopView:(UIView *)View
    {
        [View.layer removeAllAnimations];
    }
    
    #pragma mark ----- 抖动
    /// 抖动 Shake
    + (void)x_AnimaionShakeView:(UIView *)view repeatCount:(NSInteger)repeatCounts
    {
        //创建动画
        CAKeyframeAnimation *keyAnimaion = [CAKeyframeAnimation animation];
        keyAnimaion.keyPath = @"transform.rotation";
        keyAnimaion.values = @[
            @(-3 / 180.0 * M_PI),
            @(3 / 180.0 * M_PI),
            @(-3 / 180.0 * M_PI),
            @(0 / 180.0 * M_PI)
        ];//度数转弧度
        keyAnimaion.removedOnCompletion = NO;
        keyAnimaion.fillMode = kCAFillModeForwards;
        keyAnimaion.duration = 0.3;
        keyAnimaion.repeatCount = 1;//动画次数
        [view.layer addAnimation:keyAnimaion forKey:nil];
    }
    
    
    /**
     *  这里的 View 是你要搞的 View,
     */
    + (void)x_addAnimationCurlView:(UIView *)view
    {
        // 翻页
        [UIView beginAnimations:@"Curl"context:nil];//动画开始
        [UIView setAnimationDuration:0.75];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:view cache:YES];
        //[view removeFromSuperview];
        [UIView commitAnimations];
    }
    
    
    
    /**
     *  这里的 View 是你要搞的 View, indexs  区间 0-5
     */
    + (void)x_addAnimationView:(UIView *)View suckEffect:(NSInteger)indexs
    {
        /*
         这里的suckEffect就是效果名称,可以用的效果主要有:
         pageCurl 向上翻一页
         pageUnCurl 向下翻一页
         rippleEffect 滴水效果
         suckEffect 收缩效果,如一块布被抽走
         cube 立方体效果
         oglFlip 上下翻转效果
         */
        
        NSString *suckEffectStr = @"pageCurl";
        if (indexs == 0) {
            suckEffectStr = @"pageCurl";
        }
        if (indexs == 1) {
            suckEffectStr = @"pageUnCurl";
        }
        if (indexs == 2) {
            suckEffectStr = @"rippleEffect";
        }
        if (indexs == 3) {
            suckEffectStr = @"suckEffect";
        }
        if (indexs == 4) {
            suckEffectStr = @"cube";
        }
        if (indexs == 5) {
            suckEffectStr = @"oglFlip";
        }
        if (indexs > 5) {
            suckEffectStr = @"oglFlip";
        }
        
        CATransition *animation = [CATransition animation];
        [animation setDuration:1.25f];
        [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
        [animation setType:kCATransitionReveal];
        [animation setSubtype:kCATransitionFromBottom];
        [View.layer addAnimation:animation forKey:@"Reveal"];
        //还有一种设置动画类型的方法,不用setSubtype,只用setType
        [animation setType:suckEffectStr];
    }
    
    
    @end
    
    

    相关文章

      网友评论

        本文标题:View 动画

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