我有一个 Rec,我可以在手势动作中移动它,但问题是手势开始在其框架外捕获手势,我希望它只是在框架内工作。这是有关问题的代码和gif:
动图:
代码:
struct ContentView: View {
@State private var recLocation: CGFloat = CGFloat()
@GestureState private var recTranslation: CGFloat = CGFloat()
var body: some View {
GeometryReader { geometry in
Rectangle().fill(Color.red).frame(width: 50, height: 50, alignment: .center)
.position(x: recLocation + recTranslation + 25, y: geometry.size.height/2)
.gesture( DragGesture()
.updating($recTranslation) { value, state, translation in
state = value.translation.width
}
.onEnded { value in
recLocation = recLocation + value.translation.width
} )
}
}
}
Run Code Online (Sandbox Code Playgroud) 我试图使这个customRound函数,但 Xcode 抱怨无法将类型 () 的值转换为 Double
我怎么可能解决这个问题?
extension Double {
func customRound(zeros: Int) -> Double {
return round(Double(zeros)*self) / Double(zeros)
}
}
Run Code Online (Sandbox Code Playgroud)
更新:
extension Double {
func customRound(digits: Int) -> Double {
return Darwin.round(pow(10.0, Double(digits))*self) / pow(10.0, Double(digits))
}
}
Run Code Online (Sandbox Code Playgroud)