【发布时间】:2019-11-14 17:00:42
【问题描述】:
不熟悉 SwiftUI,而且关于这个新框架的文档还很少。我想知道是否有人熟悉如何在 SwiftUI 中为 Path 设置动画。
例如给定一个视图,让我们说这个简单的RingView:
struct RingView : View {
var body: some View {
GeometryReader { geometry in
Group {
// create outer ring path
Path { path in
path.addArc(center: center,
radius: outerRadius,
startAngle: Angle(degrees: 0),
endAngle: Angle(degrees: 360),
clockwise: true)
}
.stroke(Color.blue)
// create inner ring
Path { path in
path.addArc(center: center,
radius: outerRadius,
startAngle: Angle(degrees: 0),
endAngle: Angle(degrees: 180),
clockwise: true)
}
.stroke(Color.red)
.animation(.basic(duration: 2, curve: .linear))
}
}
.aspectRatio(1, contentMode: .fit)
}
}
显示的是:
现在我想知道如何为内环设置动画,即蓝线内的红线。我要做的动画将是一个简单的动画,其中路径从开始出现并遍历到结束。
使用 CoreGraphics 和旧的 UIKit 框架相当简单,但看起来不像在内部路径中添加一个简单的 .animation(.basic(duration: 2, curve: .linear)) 并使用 withAnimation 块显示视图可以做任何事情。
我查看了 Apple 提供的关于 SwiftUI 的教程,但它们实际上只涵盖了更深入的视图上的移动/缩放动画,例如 Image。
关于如何在 SwiftUI 中为 Path 或 Shape 设置动画的任何指导?
【问题讨论】: