zoukankan      html  css  js  c++  java
  • 给tabBarItem加点击效果动画

    获取到tabBarItem,添加喜欢的动画

    .h文件

    @interface JGTabBarController ()
    
    //记录上一次点击tabbar
    @property (nonatomic, assign) NSInteger indexFlag;
    
    @end

     .m文件

    ①先放大,再缩小;②Z轴旋转 ;③向上移动;④放大并保持
    @implementation JGTabBarController
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        self.indexFlag = 0;
        
    }
    
    
    #pragma mark - UITabBarDelegate
    
    - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
    {
        /** 给 tabBarButton 加动画 */
        NSInteger index = [self.tabBar.items indexOfObject:item];
        if (index != self.indexFlag) {
            //执行动画
            NSMutableArray *arry = [NSMutableArray array];
            for (UIView *btn in self.tabBar.subviews) {
                if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
                    [arry addObject:btn];
                }
            }
            //添加动画
            [self addUpTranslationAnimtaionWithArr:arry index:index];
            
            self.indexFlag = index;
        }
    }
    
    #pragma mark - More Animation
    
    /// 先放大,再缩小
    - (void)addScaleAnimtaionWithArr:(NSMutableArray *)arry index:(NSInteger)index
    {
        //放大效果,并回到原位
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        //速度控制函数,控制动画运行的节奏
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        animation.duration = 0.2;       //执行时间
        animation.repeatCount = 1;      //执行次数
        animation.autoreverses = YES;    //完成动画后会回到执行动画之前的状态
        animation.fromValue = [NSNumber numberWithFloat:0.7];   //初始伸缩倍数
        animation.toValue = [NSNumber numberWithFloat:1.3];     //结束伸缩倍数
        [[arry[index] layer] addAnimation:animation forKey:nil];
    }
    
    /// Z轴旋转
    - (void)addRotationAnimtaionWithArr:(NSMutableArray *)arry index:(NSInteger)index
    {
        //z轴旋转180度
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        //速度控制函数,控制动画运行的节奏
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        animation.duration = 0.2;       //执行时间
        animation.repeatCount = 1;      //执行次数
        animation.removedOnCompletion = YES;
        animation.fromValue = [NSNumber numberWithFloat:0];   //初始伸缩倍数
        animation.toValue = [NSNumber numberWithFloat:M_PI];     //结束伸缩倍数
        [[arry[index] layer] addAnimation:animation forKey:nil];
    }
    
    /// 向上移动
    - (void)addUpTranslationAnimtaionWithArr:(NSMutableArray *)arry index:(NSInteger)index
    {
        //向上移动
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
        //速度控制函数,控制动画运行的节奏
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        animation.duration = 0.2;       //执行时间
        animation.repeatCount = 1;      //执行次数
        animation.removedOnCompletion = YES;
        animation.fromValue = [NSNumber numberWithFloat:0];   //初始伸缩倍数
        animation.toValue = [NSNumber numberWithFloat:-10];     //结束伸缩倍数
        [[arry[index] layer] addAnimation:animation forKey:nil];
    }
    
    
    /// 放大并保持
    - (void)addscaleAndKeepAnimtaionWithArr:(NSMutableArray *)arry index:(NSInteger)index
    {
        //放大效果
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        //速度控制函数,控制动画运行的节奏
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        animation.duration = 0.2;       //执行时间
        animation.repeatCount = 1;      //执行次数
        animation.removedOnCompletion = NO;
        animation.fillMode = kCAFillModeForwards;           //保证动画效果延续
        animation.fromValue = [NSNumber numberWithFloat:1.0];   //初始伸缩倍数
        animation.toValue = [NSNumber numberWithFloat:1.15];     //结束伸缩倍数
        [[arry[index] layer] addAnimation:animation forKey:nil];
        //移除其他tabbar的动画
        for (int i = 0; i<arry.count; i++) {
            if (i != index) {
                [[arry[i] layer] removeAllAnimations];
            }
        }
    }

    @end

     UITabbarItem自定义  先放大再复原

    //添加动画
    - (void)cusTabbarItemAddAnimationWithBtn:(UIButton *)btn {
        
        CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        pulse.duration = 0.2;
        pulse.repeatCount= 1;
        pulse.autoreverses= YES;
        pulse.fromValue= [NSNumber numberWithFloat:0.7];
        pulse.toValue= [NSNumber numberWithFloat:1.3];
        [btn.layer addAnimation:pulse forKey:nil];
    }
  • 相关阅读:
    常见的消息队列中间件介绍
    关系型数据库和非关系型数据库区别、oracle与mysql的区别
    SQL Server 和 Oracle 以及 MySQL 数据库
    Redis,Memcache,MongoDb的特点与区别
    详解布隆过滤器的原理,使用场景和注意事项
    Redis缓存穿透、缓存击穿以及缓存雪崩
    RPC、HTTP、RESTful
    集群,分布式,微服务概念和区别理解
    电脑双屏变单屏后,界面显示问题
    JDK 15已发布,你所要知道的都在这里!
  • 原文地址:https://www.cnblogs.com/GJ-ios/p/10642310.html
Copyright © 2011-2022 走看看