I am building an app that is basically a shot chart for basketball teams. I have an image of a basketball court and the user taps on the court and a dot shows up. I get the tap coordinates and render a dot. Pretty simple.
Where I am getting hung up is that I need to ultimately render these taps precisely where they are on the court of the phone that recorded them across different devices. So the iPad and iPhone 14 Pro Max will need to show the same exact place at the free throw line as the iPhone SE it was recorded on, for example.
I tried converting the x/y coordinates to fractions based on image dimensions but that didn't work because the aspect ratio of the devices aren't the same.
This is probably a simple problem, but I am stuck!
EDIT:
Here is the simple code from my VC:
override func viewDidLoad() {
super.viewDidLoad()
// Add gesture recognizer
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(tapGestureRecognizer)
// I don't add this in until the second launch, after I tap and get the % below
// let dotImage = UIImageView(image: UIImage(named: "playDot"))
// dotImage.frame.origin = CGPoint(x: self.imageView.frame.size.width * 0.5007496251874063, y: self.imageView.frame.size.height * 0.20666666666666667)
// self.imageView.addSubview(dotImage)
}
@objc func imageTapped(tapGestureRecognizer: UITapGestureRecognizer) {
let touchPoint = tapGestureRecognizer.location(in: self.imageView)
let dotImage = UIImageView(image: UIImage(named: "playDot"))
dotImage.center = touchPoint
self.imageView.addSubview(dotImage)
print("Tap dot x%:\(touchPoint.x / self.imageView.frame.size.width), y%: \(touchPoint.y / self.imageView.frame.size.height)")
}
After I tap once the dot shows up like this:
And this is the console output:
Tap dot x%:0.5007496251874063, y%: 0.20666666666666667
So then I uncomment out the initial image placement code above and run the app again on the same simulator:
I want it to be exactly in the same position across multiple devices, but I can't even get it right for the same device :/