iOS动画(Core Animation)

2023-05-16

一、CABaseAnimation 

/**
 移动动画
 */
- (void)testPositionAnimation
{
    CGFloat tempViewWidth = 50;
    CGFloat tempViewY = 300;
    
    UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, tempViewY, tempViewWidth, tempViewWidth)];
    tempView.backgroundColor = [UIColor redColor];
    [self.view addSubview:tempView];
    
    //position 移动
    
    
    CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"position"];
    anima.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, tempViewY)];
    anima.toValue = [NSValue valueWithCGPoint:CGPointMake(JnScreenWidth-tempViewWidth/2, tempViewY)];
    anima.duration = 1.0f;
    ///*如果实现下面属性,在动画执行完毕后,图层会保持显示动画执行后的状态
    anima.fillMode = kCAFillModeForwards;
    anima.removedOnCompletion = NO;
    //*/
    anima.repeatCount = 20;
    [tempView.layer addAnimation:anima forKey:@"positionAnimation"];
}


/**
 旋转动画
 */
- (void)testTransRotationformAnimation
{
    CGFloat tempViewWidth = 50;
    CGFloat tempViewY = 300;
    
    UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake((JnScreenWidth-tempViewWidth)/2, tempViewY, tempViewWidth, tempViewWidth)];
    tempView.backgroundColor = [UIColor redColor];
    [self.view addSubview:tempView];
    
    //transform.rotation.x  /  y  / z  旋转
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.duration = 2;
    animation.repeatCount = 20;
    animation.beginTime = CACurrentMediaTime() + 1; // 1秒后执行
    animation.fromValue = [NSNumber numberWithFloat:0.0]; // 起始角度
    animation.toValue = [NSNumber numberWithFloat:M_PI]; // 终止角度
    [tempView.layer addAnimation:animation forKey:@"transformRotationAnimation"];
}


/**
 缩放动画
 */
- (void)testTransScaleformAnimation
{
    CGFloat tempViewWidth = 50;
    CGFloat tempViewY = 300;
    
    UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake((JnScreenWidth-tempViewWidth)/2, tempViewY, tempViewWidth, tempViewWidth)];
    tempView.backgroundColor = [UIColor redColor];
    [self.view addSubview:tempView];
    
    //transform.scale  缩放
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    animation.duration = 1.0; // 动画持续时间
    animation.repeatCount = 20; // 重复次数
    animation.fromValue = [NSNumber numberWithFloat:1.0]; // 开始时的倍率
    animation.toValue = [NSNumber numberWithFloat:1.0]; // 结束时的倍率
    
    [tempView.layer addAnimation:animation forKey:@"transformScaleAnimation"];
}


/**
 渐隐动画
 */
- (void)testOpacityAnimation
{
    CGFloat tempViewWidth = 50;
    CGFloat tempViewY = 300;
    
    UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake((JnScreenWidth-tempViewWidth)/2, tempViewY, tempViewWidth, tempViewWidth)];
    tempView.backgroundColor = [UIColor redColor];
    [self.view addSubview:tempView];
    
    //opacity 指layer的透明度
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    basicAnimation.fromValue = @(1.0);
    basicAnimation.toValue  = @(0.0); //[NSNumber numberWithFloat:0.0]
    basicAnimation.duration = 2;
    basicAnimation.repeatCount = 20;
    [tempView.layer addAnimation:basicAnimation forKey:@"opacityAnimation"];
}

二、CAKeyframeAnimation

- (void)testPathAnimation
{
    
    CGFloat tempViewWidth = 50;
    CGFloat tempViewY = 300;
    
    UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake((JnScreenWidth-tempViewWidth)/2, tempViewY, tempViewWidth, tempViewWidth)];
    tempView.backgroundColor = [UIColor redColor];
    [self.view addSubview:tempView];
    
   
    /*
     //矩形
     + (instancetype)bezierPathWithRect:(CGRect)rect;
     
     //以矩形框为切线画圆
     + (instancetype)bezierPathWithOvalInRect:(CGRect)rect;
     
     //带圆角的矩形框
     + (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius;
     
     //画圆弧
     + (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;
     */
     CAKeyframeAnimation *anima = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(JnScreenWidth/2-100, JnScreenWidth/2-100, 200, 200)];
    anima.path = path.CGPath;
    anima.duration = 2.0f;
    anima.repeatCount = 20;
    [tempView.layer addAnimation:anima forKey:@"pathAnimation"];
}

三、CAAnimationGroup

- (void)testGroupAnimation
{
    
    CGFloat tempViewWidth = 50;
    CGFloat tempViewY = 300;
    
    UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake((JnScreenWidth-tempViewWidth)/2, tempViewY, tempViewWidth, tempViewWidth)];
    tempView.backgroundColor = [UIColor redColor];
    [self.view addSubview:tempView];
    
    CAKeyframeAnimation *anima1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    NSValue *value0 = [NSValue valueWithCGPoint:CGPointMake(0, JnScreenHeight/2-50)];
    NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(JnScreenWidth/3, JnScreenHeight/2-50)];
    NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(JnScreenWidth/3, JnScreenHeight/2+50)];
    NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(JnScreenWidth*2/3, JnScreenHeight/2+50)];
    NSValue *value4 = [NSValue valueWithCGPoint:CGPointMake(JnScreenWidth*2/3, JnScreenHeight/2-50)];
    NSValue *value5 = [NSValue valueWithCGPoint:CGPointMake(JnScreenWidth, JnScreenHeight/2-50)];
    anima1.values = [NSArray arrayWithObjects:value0,value1,value2,value3,value4,value5, nil];
    //缩放动画
    CABasicAnimation *anima2 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    anima2.fromValue = [NSNumber numberWithFloat:0.8f];
    anima2.toValue = [NSNumber numberWithFloat:2.0f];
    //旋转动画
    CABasicAnimation *anima3 = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    anima3.toValue = [NSNumber numberWithFloat:M_PI*4];
    //组动画
    CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
    groupAnimation.animations = [NSArray arrayWithObjects:anima1,anima2,anima3, nil];
    groupAnimation.duration = 4.0f;
    [tempView.layer addAnimation:groupAnimation forKey:@"groupAnimation"];
    
}

四、CATransition

@property (nonatomic, assign) NSInteger index;
@property (nonatomic, weak) UIImageView *imageView;


- (UIImageView *)imageView
{
    if (!_imageView)
    {
        UIImageView *mv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];
        mv.frame = CGRectMake(30, 60, JnScreenWidth - 60, JnScreenHeight-120);
        mv.userInteractionEnabled = YES;
        [self.view addSubview:mv];
        _imageView = mv;
    }
    return _imageView;
}

- (void)testTransitionWithSubType:(NSString *)subtype
{
    //CATransition
    //------------- type:动画过渡类型 -------------
    /* Apple官方的SDK其实只提供了四种过渡效果。
    kCATransitionFade 渐变效果
    kCATransitionMoveIn 进入覆盖效果
    kCATransitionPush 推出效果
    kCATransitionReveal 揭露离开效果
     */
    /* 这类是API引入的,在苹果官网是不会承认的,所以不建议使用
     cube 立方体效果
     suckEffect 犹如一块布被抽走
     oglFlip上 下翻转效果
     rippleEffect 滴水效果
     pageCurl 向左翻页
     pageUnCurl 向下翻页
     */
    
    //------------- subtype:动画过渡方向 -------------
    /*
     kCATransitionFromRight 从右侧进入
     kCATransitionFromLeft 从左侧进入
     kCATransitionFromTop 从顶部进入
     kCATransitionFromBottom 从底部进入
     */
    
    /*
     startProgress:动画起点(在整体动画的百分比)
     endProgress:动画终点(在整体动画的百分比)
     */
    CATransition *transition = [CATransition animation];
    //动画类型
    transition.type = @"cube";//立体翻转
    //动画时间
    transition.duration = 1;
    //子类型
    transition.subtype = subtype;
    //添加动画
    [self.imageView.layer addAnimation:transition forKey:nil];
    //切换图片
    self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",(long)index]];
}

/**
 *  添加手势
 */
- (void)addTransitionGesture
{
    //每个轻扫手势对象只支持一个方向
    //向右
    UISwipeGestureRecognizer *rightSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    //设置从左向右滑
    rightSwipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
    [self.imageView addGestureRecognizer:rightSwipeGesture];
    
    //向左
    UISwipeGestureRecognizer *leftSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    //设置从右向左滑
    leftSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.imageView addGestureRecognizer:leftSwipeGesture];
}

/**
 *  轻扫切换图片
 *
 *  @param swipeGesture swipeGesture description
 */
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipeGesture
{
    NSString *subType;
    //判断方向
    if (swipeGesture.direction == UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"向右滑");
        //上张图片
        self.index--;
        subType = kCATransitionFromLeft;
    }else{
        NSLog(@"向左滑");
        //下张图片
        self.index++;
        subType = kCATransitionFromRight;
    }
    [self testTransitionWithSubType:subType];
}
self.index = 1;
[self addTransitionGesture];

Demo地址:https://github.com/JnKindle/CoreAnimation

欢迎大家访问我的GitHub

GitTub:https://github.com/JnKindle

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iOS动画(Core Animation) 的相关文章

随机推荐

  • gradle学习二 利用javassist api修改class字节码

    一 前言 Javassist Java Programming Assistant makes Java bytecode manipulation simple It is a class library for editing byte
  • AirPlay Android接收端学习一 协议

    一 AirPlay 接收端描述 AirPlay是苹果的私有协议 xff0c 苹果官方未开放api和sdk xff0c 目前相对权威的一份非官方协议文档 这篇文档详细描述了aiplay 服务发现 图片 音视频 镜像投屏的相关协议 xff0c
  • android手机 加速度传感器 获取x,y,z轴上的加速度

    package com zhp andorid import android app Activity import android content Context import android hardware Sensor import
  • 声纹识别调研

    1 基础概念 声纹 Voiceprint xff0c 是用电声学仪器显示的携带言语信息的声波频谱 现代科学研究表明 xff0c 声纹不仅具有特定性 xff0c 而且有相对稳定性的特点 成年以后 xff0c 人的声音可保持长期相对稳定不变 实
  • Gradle学习三 :AS自定义Gradle插件

    一 定义插件 1 File New Module Android Library 取名plugin1 2 删除plugin1目录下所有文件 xff0c 只保留build gradle 编写build gradle 代码如下 xff1a ap
  • Android Hook 一 Hook CloseGuard

    一 抛出问题 在上一篇 dalvik system CloseGuard 介绍了CloseGuard的原理和作用 xff0c 并在文中提到 APP端可以利用Hook REPORTER 在来实现客制化的上报提示信息 本章通过代码来学一下怎样H
  • Python小白学习笔记-day3

    第三章 Python基本数据类型 学习笔记 浮点数与整数 xff0c Python浮点数运算存在 不确定尾数 问题 xff0c 即两个浮点数运算 xff0c 有一定概率在运算结果后增加一些 不确定的 尾数 xff08 受限于计算机表示浮点数
  • Java实现凯撒密码

    Java实现凯撒密码 加密和解密代码 根据公式 C 61 xff08 P 43 key mod 26 P 61 xff08 C 43 key 1 mod 26 key 1 61 26 key 26 可知 c 61 char c a 43 k
  • Pycharm使用pip报错:Script file ‘D:\Anaconda3\envs\pytorch\Scripts\pip-script.py‘ is not present

    问题描述 xff1a 报错情况1 xff1a 使用pip安装报错 xff1a Script file D Anaconda3 envs pytorch Scripts pip script py is not present 报错情况2 x
  • pip提示版本低需要升级,WARNING: You are using pip version 20.1.1; however,version 20.2.3 is available.

    警告 xff1a 您使用的是pip版本20 1 1 xff1b 但是 xff0c 版本20 2 3是可用的 You should consider upgrading via the 39 e python38 python exe m p
  • manjaro换源

    安装Linux系统必定先换源 xff01 xff01 xff01 x1f601 换源这一块 xff0c 我认为debian系 xff08 如Ubuntu xff09 以及Red Hat系 xff08 如fedora xff09 与基于Arc
  • WARNING: Ignoring invalid distribution -ip (d:\python3.7.5\lib\site-packages)

    警告原因 xff1a 之前安装插件失败 中途退出 xff0c 导致插件安装出现异常导致的 解决方法 xff1a 找到警告信息中报错的目录 xff0c 然后删掉 开头的文件夹
  • Linux上安装ntp

    问题 要同步6台服务器上的时间 xff0c 主要为了以后安装大数据软件做准备 xff0c 这里就需要安装ntp软件 解决 安装ntp和配置环境 1 gt 准备工作 xff1a 关闭防火墙 xff08 一定要关防火墙 xff0c 我是直接永久
  • ubuntu20.04上安装mysql

    目录 安装卸载 安装 命令安装 xff1a 更新源 sudo apt update 默认下载 xff0c 因为下载其他版本太复杂了 xff0c 这里默认是mysql8 0 29 sudo apt install mysql server y
  • arch安装和配置

    问题 安装arch 这里就不赘述相关的步骤 xff0c 我是看b站一个视频 xff0c 下面会给出网址 xff0c 但是要提示一下 xff0c 网络部分是错误的 xff0c 要更改一下 安装之后要配置相关的东西 xff0c 安装完成之后就是
  • (记录)电脑维修指南

    问题 自己在2020年的时候花了将近1500块自己组装了一台计算机 xff0c 但是经常出现蓝屏 xff0c 自动重启 xff0c 并且显示硬件问题 xff0c 2022年的时候也拆开过修过一次 xff0c 也和客服battle了几次 xf
  • 代码复现问题以及解决

    问题 cuda版本不匹配 xff0c 对于版本不匹配问题真的很难受CPU版本还有GPU版本问题包安装 xff0c apex加速的安装 xff0c transformer版本也会限制python的版本程序运行 xff0c 如果程序写的是比较好
  • one-hot向量形式

    one hot向量 one hot向量将类别变量转换为机器学习算法易于利用的一种形式的过程 xff0c 这个向量的表示为一项属性的特征向量 xff0c 也就是同一时间只有一个激活点 xff08 不为0 xff09 xff0c 这个向量只有一
  • c语言中的位移位操作

    先要了解一下C语言里所有的位运算都是指二进制数的位运算 即使输入的是十进制的数 xff0c 在内存中也是存储为二进制形式 lt lt 用法 xff1a 格式是 xff1a a lt lt m xff0c a和m必须是整型表达式 xff0c
  • iOS动画(Core Animation)

    一 CABaseAnimation 移动动画 void testPositionAnimation CGFloat tempViewWidth 61 50 CGFloat tempViewY 61 300 UIView tempView 6