gpt4 book ai didi

ios - 是否可以在 SwiftUI 中对某个路径上的 View 进行动画处理

转载 作者:行者123 更新时间:2023-11-29 05:11:22 26 4
gpt4 key购买 nike

是否可以在 SwiftUI 中对某个路径上的 View 进行动画处理。在 SwiftUI 之前,只需设置其图层的路径属性即可。我现在怎样才能做类似的事情?

编辑这是我如何使用 UIKit 做到这一点的示例

let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)

let animation = CAKeyframeAnimation(keyPath: #keyPath(CALayer.position))
animation.duration = CFTimeInterval(duration)
animation.repeatCount = 1
animation.path = path.cgPath
animation.isRemovedOnCompletion = true
animation.fillMode = .forwards
animation.timingFunction = CAMediaTimingFunction(name: .linear)

viewToAnimate.layer.add(animation, forKey: "someAnimationName")

最佳答案

这里是一个可能方法的演示(只是一个草图:许多参数只是硬编码的,但它们可以通过属性、构造函数、回调等进行配置)

SwiftUI KeyframeAnimation

struct PathAnimatingView<Content>: UIViewRepresentable where Content: View {
let path: Path
let content: () -> Content

func makeUIView(context: UIViewRepresentableContext<PathAnimatingView>) -> UIView {
let view = UIView(frame: .zero)
view.translatesAutoresizingMaskIntoConstraints = false
view.layer.borderColor = UIColor.red.cgColor
view.layer.borderWidth = 2.0

let animation = CAKeyframeAnimation(keyPath: #keyPath(CALayer.position))
animation.duration = CFTimeInterval(3)
animation.repeatCount = 3
animation.path = path.cgPath
animation.isRemovedOnCompletion = false
animation.fillMode = .forwards
animation.timingFunction = CAMediaTimingFunction(name: .linear)

let sub = UIHostingController(rootView: content())
sub.view.translatesAutoresizingMaskIntoConstraints = false

view.addSubview(sub.view)
sub.view.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
sub.view.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

view.layer.add(animation, forKey: "someAnimationName")
return view
}

func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<PathAnimatingView>) {
}

typealias UIViewType = UIView
}

struct TestAnimationByPath: View {
var body: some View {
VStack {
PathAnimatingView(path: Circle().path(in:
CGRect(x: 100, y: 100, width: 100, height: 100))) {
Text("Hello, World!")
}
}
}
}

关于ios - 是否可以在 SwiftUI 中对某个路径上的 View 进行动画处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59648676/

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