OGeek|极客世界-中国程序员成长平台

标题: ios - 是否可以继承 CAAnimation? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 11:04
标题: ios - 是否可以继承 CAAnimation?

我只想通过中心点 C 和半径 R 的圆弧从 A 点到 B 点对图层的 position 属性进行动画处理。如此简单,但我觉得我正在尝试弯曲 Core Animation一半来做到这一点。

似乎理想的方法是简单地创建一个自定义 CAPropertyAnimation 子类,例如:

let anim = MyArcPropertyAnimation(keyPath: "position", center: CGPoint, radius: CGFloat)
anim.fromValue = fromPoint
anim.toValue = toPoint
myLayer.addAnimation(anim, forKey: "positionArcAnimation")

但我找不到任何关于子类化 CAAnimation 或其任何派生类的信息。 bjc(cs)CAValueFunction" rel="noreferrer noopener nofollow">CAValueFunction 似乎也是如此这似乎也很有希望。

注意:我使用 POP 运行它很容易,但发现,as warned by a facebook engineer ,性能对我来说太慢了。



Best Answer-推荐答案


文档似乎暗示 CAAnimation 不是为子类化而设计的。此外,大多数动画都是在窗口服务器(称为 backboardd)中执行的,您肯定无法让 backboardd 来执行您的代码。

不管怎样,听起来你可以使用 CAKeyframeAnimation 获得你想要的效果。将 CAKeyframeAnimationpath 属性设置为您希望图层遵循的弧线。 Playground 示例:

import UIKit
import XCPlayground

let view = UIView(frame: CGRectMake(0, 0, 400, 400))
view.backgroundColor = .whiteColor()
let mover = UIView(frame: CGRectMake(0, 0, 10, 10))
mover.backgroundColor = .redColor()
view.addSubview(mover)
XCPlaygroundPage.currentPage.liveView = view

let animation = CAKeyframeAnimation(keyPath: "position")
let path = UIBezierPath(arcCenter: CGPointMake(200, 200), radius: 160, startAngle: 1, endAngle: 5, clockwise: true)
animation.path = path.CGPath
animation.duration = 2
animation.calculationMode = kCAAnimationPaced
mover.layer.addAnimation(animation, forKey: "position")
mover.center = path.currentPoint

结果:

playground demo

关于ios - 是否可以继承 CAAnimation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36855798/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4