gpt4 book ai didi

ios - 是否可以继承 CAAnimation?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:57:01 27 4
gpt4 key购买 nike

我只想通过圆心 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 或其任何派生类的任何信息。 CAValueFunction 似乎也是如此这在其他方面似乎也很有希望。

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

最佳答案

文档中似乎暗示 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com