zoukankan      html  css  js  c++  java
  • iOS tabbar点击动画效果实现

    正常情况下,我们点击tabbar都只有一个变色效果,但有时候,如果我们想给它添加一个点击动画,该如何做呢?

    先上几个效果图:

    1、先放大,再缩小                     2、Z轴旋转

                 

    3、Y轴位移                          4、放大并保持

               

    原理:利用UITabBarController实现,在tabbar的 didSelectItem 代理里添加动画效果。

    下面就以上几种场景贴上代码:

    准备代码:

    @interface MainTabbarVC ()<UITabBarControllerDelegate>
    @property (nonatomic,assign) NSInteger  indexFlag;  //记录上一次点击tabbar,使用时,记得先在init或viewDidLoad里 初始化 = 0
    @end

    -(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{ 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.indexFlag = index; } }

    1、先放大,再缩小

    //放大效果,并回到原位
    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];

    2、Z轴旋转

    //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];

    3、Y轴位移

    //向上移动
    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];

    4、放大并保持

    //放大效果
    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];
        }
    }

    此外,如果想定制其他动画效果,还可以从下面属性里自己定制动画

    谢谢!~

  • 相关阅读:
    设计模式——观察者模式
    安卓xml动画
    部署在weblogic上的springboot项目上传文件(servlet方式)
    Spring Boot 部署到weblogic 12c
    SpingBoot+Druid监控页面打不开(404)
    DAY49-前端入门-浮动布局案例、z-index、flex布局、响应式布局、过渡与动画
    DAY48-前端入门-文档流、浮动布局、清浮动、流式布局、定位布局
    DAY46-前端入门-组合选择器、标签a_img_list、盒模型、伪类、盒模型布局
    DAY45-前端入门-css的三种引用方式以及优先级、样式与长度颜色、常用样式、css选择器
    DAY44-前端入门-前端三剑客、第一个页面、常用标签、标签分类
  • 原文地址:https://www.cnblogs.com/yajunLi/p/6288811.html
Copyright © 2011-2022 走看看