gpt4 book ai didi

ios - CALayer 路径动画不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 06:00:45 24 4
gpt4 key购买 nike

为什么这个路径动画不起作用?

let frame = CGRect(x: 20, y: 20, width: 10, height: 10)
let frame2 = CGRect(x: 20, y: 20, width: 100, height: 100)

let path = UIBezierPath(rect: frame)
let path2 = UIBezierPath(rect: frame2)

let animation = CABasicAnimation(keyPath: "path")
animation.fromValue = path
animation.toValue = path2
animation.duration = 3
animation.repeatCount = Float.greatestFiniteMagnitude

let l = CAShapeLayer()
l.path = path.cgPath
l.fillColor = UIColor.black.cgColor
self.view.layer.addSublayer(l)

l.add(animation, forKey: "pathAnimation")

完整的 Playground :

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {

override func loadView() {
let view = UIView()
view.backgroundColor = .white
self.view = view

let frame = CGRect(x: 20, y: 20, width: 10, height: 10)
let frame2 = CGRect(x: 20, y: 20, width: 100, height: 100)

let path = UIBezierPath(rect: frame)
let path2 = UIBezierPath(rect: frame2)

let animation = CABasicAnimation(keyPath: "path")
animation.fromValue = path
animation.toValue = path2
animation.duration = 3
animation.repeatCount = Float.greatestFiniteMagnitude

let l = CAShapeLayer()
l.path = path.cgPath
l.fillColor = UIColor.black.cgColor
self.view.layer.addSublayer(l)

l.add(animation, forKey: "pathAnimation")
}
}

PlaygroundPage.current.liveView = MyViewController()

最佳答案

你应该在动画中使用cgPath

    animation.fromValue = path.cgPath
animation.toValue = path2.cgPath

关于ios - CALayer 路径动画不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51754753/

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