如何仅用一根手指在 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 轴移动时才有效......

当您围绕要旋转的视图进行圆周运动时,我希望解决方案起作用...

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...