非原创、ChatGPT 问答生成,背景是看到 CWMoney App 的动画诱人,该App内置了POP开源库,了解一下。

在 iOS 中,可以使用 Core Animation 框架(如 CAAnimation)来实现图标的旋转和跳动动画。下面是一个使用 Swift 和 Core Animation 实现图标旋转和跳动的示例。

旋转动画

首先,我们来看如何实现图标的旋转动画:

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        rotateAnimation()
    }

    func rotateAnimation() {
        let rotation = CABasicAnimation(keyPath: "transform.rotation")
        rotation.toValue = NSNumber(value: Double.pi * 2)
        rotation.duration = 1.0
        rotation.isCumulative = true
        rotation.repeatCount = Float.infinity
        imageView.layer.add(rotation, forKey: "rotationAnimation")
    }
}

跳动动画

接下来是跳动动画的实现:

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        jumpAnimation()
    }

    func jumpAnimation() {
        let jump = CAKeyframeAnimation(keyPath: "transform.translation.y")
        jump.values = [0, -10, 0]
        jump.keyTimes = [0, 0.5, 1]
        jump.duration = 0.5
        jump.repeatCount = Float.infinity
        imageView.layer.add(jump, forKey: "jumpAnimation")
    }
}

旋转和跳动动画结合

最后,我们将旋转和跳动动画结合起来:

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        rotateAndJumpAnimation()
    }

    func rotateAndJumpAnimation() {
        // 旋转动画
        let rotation = CABasicAnimation(keyPath: "transform.rotation")
        rotation.toValue = NSNumber(value: Double.pi * 2)
        rotation.duration = 1.0
        rotation.isCumulative = true
        rotation.repeatCount = Float.infinity

        // 跳动动画
        let jump = CAKeyframeAnimation(keyPath: "transform.translation.y")
        jump.values = [0, -10, 0]
        jump.keyTimes = [0, 0.5, 1]
        jump.duration = 0.5
        jump.repeatCount = Float.infinity

        // 将两个动画组合起来
        let group = CAAnimationGroup()
        group.animations = [rotation, jump]
        group.duration = 1.0
        group.repeatCount = Float.infinity

        imageView.layer.add(group, forKey: "rotateAndJumpAnimation")
    }
}

解释

  • 旋转动画:使用 CABasicAnimation 来实现,动画的关键路径为 transform.rotation,表示围绕 Z 轴的旋转。toValue 设置为 Double.pi * 2,表示旋转一周,duration 表示动画持续时间,repeatCount 表示动画重复次数。
  • 跳动动画:使用 CAKeyframeAnimation 来实现,动画的关键路径为 transform.translation.y,表示沿 Y 轴的平移。values 设置为 [0, -10, 0],表示跳动的高度,keyTimes 设置为 [0, 0.5, 1],表示动画在时间上的分配,duration 表示动画持续时间,repeatCount 表示动画重复次数。
  • 组合动画:使用 CAAnimationGroup 将旋转和跳动动画组合起来,使其同时执行。

这样,你就可以在 iOS 应用中实现一个图标旋转和跳动的动画效果了。

问:使用 POP 开源库可以实现类似效果吗?如何实现?

可以使用 Facebook 的 POP 动画库来实现类似的旋转和跳动动画效果。POP 是一个强大的动画库,提供了丰富的动画类型和简单的接口。下面是如何使用 POP 来实现图标的旋转和跳动动画。

安装 POP

首先,通过 CocoaPods 安装 POP

  1. Podfile 中添加以下行:
   pod 'pop'
  1. 运行 pod install

旋转动画

使用 POP 实现图标的旋转动画:

import UIKit
import pop

class ViewController: UIViewController {
    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        rotateAnimation()
    }

    func rotateAnimation() {
        let rotationAnimation = POPBasicAnimation(propertyNamed: kPOPLayerRotation)
        rotationAnimation?.toValue = NSNumber(value: Double.pi * 2)
        rotationAnimation?.duration = 1.0
        rotationAnimation?.repeatForever = true
        imageView.layer.pop_add(rotationAnimation, forKey: "rotationAnimation")
    }
}

跳动动画

使用 POP 实现图标的跳动动画:

import UIKit
import pop

class ViewController: UIViewController {
    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        jumpAnimation()
    }

    func jumpAnimation() {
        let jumpAnimation = POPSpringAnimation(propertyNamed: kPOPLayerPositionY)
        jumpAnimation?.fromValue = imageView.layer.position.y
        jumpAnimation?.toValue = imageView.layer.position.y - 10
        jumpAnimation?.springBounciness = 20
        jumpAnimation?.repeatForever = true
        imageView.layer.pop_add(jumpAnimation, forKey: "jumpAnimation")
    }
}

旋转和跳动动画结合

将旋转和跳动动画结合起来:

import UIKit
import pop

class ViewController: UIViewController {
    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        rotateAndJumpAnimation()
    }

    func rotateAndJumpAnimation() {
        // 旋转动画
        let rotationAnimation = POPBasicAnimation(propertyNamed: kPOPLayerRotation)
        rotationAnimation?.toValue = NSNumber(value: Double.pi * 2)
        rotationAnimation?.duration = 1.0
        rotationAnimation?.repeatForever = true
        imageView.layer.pop_add(rotationAnimation, forKey: "rotationAnimation")

        // 跳动动画
        let jumpAnimation = POPSpringAnimation(propertyNamed: kPOPLayerPositionY)
        jumpAnimation?.fromValue = imageView.layer.position.y
        jumpAnimation?.toValue = imageView.layer.position.y - 10
        jumpAnimation?.springBounciness = 20
        jumpAnimation?.repeatForever = true
        imageView.layer.pop_add(jumpAnimation, forKey: "jumpAnimation")
    }
}

解释

  • 旋转动画:使用 POPBasicAnimation 创建一个基本动画,属性为 kPOPLayerRotation,表示围绕 Z 轴的旋转。toValue 设置为 Double.pi * 2 表示旋转一周,duration 表示动画持续时间,repeatForever 设置为 true 表示动画无限循环。
  • 跳动动画:使用 POPSpringAnimation 创建一个弹簧动画,属性为 kPOPLayerPositionY,表示沿 Y 轴的平移。fromValue 设置为图标的初始 Y 轴位置,toValue 设置为初始位置减去 10,表示跳动的高度,springBounciness 设置为 20,表示弹簧的弹性,repeatForever 设置为 true 表示动画无限循环。
  • 组合动画:直接将两个动画添加到图标的图层上,使其同时执行。

通过上述代码,你可以使用 POP 动画库在 iOS 应用中实现图标的旋转和跳动动画。POP 动画库提供了丰富的动画类型和灵活的配置选项,使得动画效果更容易实现和控制。