如何解决如何仅用一根手指在 SwiftUI 中旋转图像?
我在视图中有一个图像,我希望只能用一根手指旋转它,我该如何在 SwiftUI 中做到这一点?
我检查了 RotationGesture 但它只能用两根手指...
谢谢
解决方法
好的,我用这个代码搞定了:
https://gist.github.com/ts95/9f8e05380824c6ca999ab3bc1ff8541f
一根手指旋转
struct RotationGesture: View {
@State var gestureValue = CGSize.zero
var body: some View {
Text("Hello,World!")
.frame(width: 150,height: 60)
.padding()
.background(Color.orange)
.cornerRadius(15)
.rotationEffect(Angle(degrees: Double(-gestureValue.width)))
.gesture(
DragGesture().onChanged({ (value) in
self.gestureValue = value.translation
}).onEnded({ (value) in
self.gestureValue = .zero
})
)
.animation(.spring(response: 0.5,dampingFraction: 0.6,blendDuration: 0))
}
}
,
好的,用这个代码修复它:
struct RotationGesture: View {
@State var totalRotation = CGSize.zero
@State var currentRotation = CGSize.zero
var body: some View {
Text("Hello,World!")
.frame(width: 150,height: 60)
.padding()
.background(Color.orange)
.cornerRadius(15)
.rotationEffect(Angle(degrees: Double(-totalRotation.width)))
.gesture(
DragGesture()
.onChanged { value in
totalRotation.width = value.translation.width + currentRotation.width
}
.onEnded { value in
currentRotation = totalRotation
}
)
}
}
但是现在我们必须修复垂直移动,因为这个解决方案只在你绕 X 轴移动时才有效......
当您围绕要旋转的视图进行圆周运动时,我希望解决方案起作用...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。