【发布时间】:2021-12-26 18:48:23
【问题描述】:
我有一个 repeatForever 动画,我想暂停动画,然后再恢复它,但我的代码不起作用,它们破坏了当前动画的帧。
struct HatchedShap: Shape {
let dis: CGFloat
var move: CGFloat
var animatableData: CGFloat {
get { return move }
set { move = newValue }
}
func path(in rect: CGRect) -> Path {
Path { path in
for start in stride(from: 0, through: rect.height, by: dis) {
let offset = start + (dis*move).truncatingRemainder(dividingBy: dis)
if (0.0...rect.height ~= offset) {
path.move(to: CGPoint(x: rect.minX, y: rect.minY + offset))
path.addLine(to: CGPoint(x: rect.maxX, y: rect.minY + offset))
}
}
}
}
}
用例:
struct ContentView: View {
@State private var move: CGFloat = 0.0
var body: some View {
HStack {
VStack {
Text(String(describing: move)).bold().animation(.none)
HatchedShap(dis: 30.0, move: move)
.stroke(Color.red, style: StrokeStyle(lineWidth: 5.0))
.background(Color.black)
.frame(width: 100, height: 200)
.onTapGesture {
if (move == 3) { move = 0}
else { move = 3 }
}
}
.animation((move != 0) ? Animation.linear(duration: 1.0).repeatForever(autoreverses: false) : Animation.linear(duration: 1.0), value: move)
}
}
}
【问题讨论】: