Getting the coordinates from the location I touch the touchscreen

后端 未结 6 1282
温柔的废话
温柔的废话 2020-12-03 05:15

I try to get the coordinates from the location where I hit the touchscreen to do put a specific UIImage at this point.

How can I do this?

6条回答
  •  Happy的楠姐
    2020-12-03 05:27

    For SwiftUI I created a new swift file called HostingController.swift

    import Foundation
    import UIKit
    import SwiftUI
    
    class HostingController: UIHostingController {
    
        override func touchesBegan(_ touches: Set, with event: UIEvent?) {
            if let touch = touches.first {
                let position = touch.location(in: view)
                print(position)
            }
        }
    }
    

    Then I changed the following lines of code in the SceneDelegate.swift

    window.rootViewController = UIHostingController(rootView: ContentView())
    

    to

    window.rootViewController = HostingController(rootView: ContentView())
    

    SwiftUI is basically wrapped in a ViewController via the UIHostingController. At least thats how I think it is.

    I hope this helps!

    Greetings krjw

提交回复
热议问题