Mof*_*waw 3 animation ios swift swiftui
我有以下情况:
我想用自定义动画翻转这个形状。我不知道如何恰当地描述它。
每当我上的箭头挖掘,应该转变成另一种。无需翻转、旋转等,只需简单变换即可。
如果这还不够准确,请随时发表评论!
struct ContentView: View {
@State private var change = false
private let arrowWidth: CGFloat = 80
var body: some View {
Path { path in
path.move(to: .zero)
path.addLine(to: CGPoint(x: arrowWidth/2, y: change ? -20 : 20))
path.move(to: CGPoint(x: arrowWidth/2, y: change ? -20 : 20))
path.addLine(to: CGPoint(x: arrowWidth, y: 0))
}
.stroke(style: StrokeStyle(lineWidth: 12, lineCap: .round))
.frame(width: arrowWidth)
.foregroundColor(.green)
.animation(.default)
.onTapGesture { change.toggle() }
.padding(.top, 300)
}
}
Run Code Online (Sandbox Code Playgroud)
正如您现在所看到的,它没有动画。我不知道该怎么做。
任何帮助深表感谢。谢谢!
注意:我不能用两个圆角矩形 + 简单地旋转来做到这一点,因为我有一个不透明动画,这使得重叠可见。
这是可能的方法 - 将路径移动到自定义形状并将更改的参数设置为可动画属性。使用 Xcode 12 / iOS 14 测试
struct MyArrow: Shape {
var width: CGFloat
var offset: CGFloat
var animatableData: CGFloat {
get { offset }
set { offset = newValue }
}
func path(in rect: CGRect) -> Path {
Path { path in
path.move(to: .zero)
path.addLine(to: CGPoint(x: width/2, y: offset))
path.move(to: CGPoint(x: width/2, y: offset))
path.addLine(to: CGPoint(x: width, y: 0))
}
}
}
struct ContentView: View {
@State private var change = false
private let arrowWidth: CGFloat = 80
var body: some View {
MyArrow(width: arrowWidth, offset: change ? -20 : 20)
.stroke(style: StrokeStyle(lineWidth: 12, lineCap: .round))
.frame(width: arrowWidth)
.foregroundColor(.green)
.animation(.default)
.onTapGesture { change.toggle() }
.padding(.top, 300)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
208 次 |
| 最近记录: |