This question is more suited for StackOverflow (and maybe Apple's dev forums) than Swift forums, so I suggest you go there instead. You might have better luck there.
I think you should try to play around with conversions a little in Swift Playground before diving into fixing the bug, at least to get some handle on the trig. If you have some iPads, this Swift Playground snippet could be useful*. Note that I use touchesMoved
instead of touchEnded
for a more interactive experience.
import UIKit
import SwiftUI
import PlaygroundSupport
class XView: UIView {
var updateText: ((LocalizedStringKey) -> ())!
var touchDown: CGPoint!
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
touchDown = touch!.location(in: self)
updateText("Origin: (\(touchDown.x, specifier: "%.2f"), \(touchDown.y, specifier: "%.2f"))\n\n")
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.location(in: self)
let touchUp = touchLocation
let angle = atan2(touchDown.y - touchUp.y, touchDown.x - touchUp.x)
updateText("""
Origin: (\(touchDown.x, specifier: "%.2f"), \(touchDown.y, specifier: "%.2f"))
Current: (\(touchUp.x, specifier: "%.2f"), \(touchUp.y, specifier: "%.2f"))
Angle: \(angle)
""")
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
touchDown = nil
updateText("")
}
}
struct XViewRepresentation: UIViewRepresentable {
@Binding var text: LocalizedStringKey
func makeUIView(context: Context) -> XView {
let uiView = XView()
uiView.updateText = { text = $0 }
return uiView
}
func updateUIView(_ uiView: UIViewType, context: Context) {}
}
struct MainView: View {
@State var text: LocalizedStringKey = ""
var body: some View {
ZStack {
Text(text)
.monospacedDigit()
.frame(width: 200, alignment: .leading)
XViewRepresentation(text: $text)
}
}
}
PlaygroundPage.current.setLiveView(MainView())
Now, depending on where you want the 0-radian angle is (and whether it increases clockwise or counterclockwise), the angle
formula above could look very different. So it depends on how you intend to use it.
As @tera mentioned cos
and sin
works with radians, so you're simply using the wrong units regardless of your intention (and things would be spinning about 60x faster than should be). Plus, that code simply moves shape
in X=Y
direction by distance
, then stretches it by a constant cos(deg)
. Since I don't know your intended transformation, I can't say much. Then again, you may have better luck asking over SO than here.
* On macOS, I'd suggest that you use Xcode's playground instead. The iOS playground on macOS has been a subpar experience for me.