I'm making a camera app using Swift. Currently, I'm working on displaying a yellow square when the user taps a screen to change the focus and exposure of the camera. I was able to make a change of camera focus and exposure by tapping somewhere on the screen. However, when I try to display the yellow square at the user tapped location, it always shows up top left corner of the iPhone. (I mean... for example if I press the center of the screen to change the focus, the camera app will focus to the center but the yellow square, which indicates where to focus, appears top left corner of the screen.)
This is the screen of the test device. As you can see, there is a yellow square at the top left corner even though I tapped the center of the screen.
Here is a part of my code.
final class ViewController: UIViewController {
@IBOutlet private weak var lfView: MTHKView!
@IBOutlet weak var recordButton: UIButton!
@IBOutlet weak var pointOfInterestView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
pointOfInterestView.layer.borderWidth = 1
pointOfInterestView.layer.borderColor = UIColor.systemYellow.cgColor
// more codes...
}
// camera focus
func setFocus(focusMode: AVCaptureDevice.FocusMode, exposureMode: AVCaptureDevice.ExposureMode, atPoint devicePoint: CGPoint, shouldMonitorSujectAreaChange: Bool) {
// camera can now change focus and exposure at tapped place
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touchPoint = touches.first! as UITouch
let screenSize = lfView.bounds.size
let focusPoint = CGPoint(x: touchPoint.location(in: lfView).y / screenSize.height, y: 1.0 - touchPoint.location(in: lfView).x / screenSize.width)
print("focus point \(focusPoint)")
showPointOfInterestViewAtPoint(point: focusPoint)
setFocus(focusMode: .autoFocus, exposureMode: .autoExpose, atPoint: focusPoint, shouldMonitorSujectAreaChange: true)
}
func showPointOfInterestViewAtPoint(point: CGPoint) {
pointOfInterestHalfCompletedWorkItem = nil
pointOfInterestComplatedWorkItem = nil
pointOfInterestView.center = point
print("point of interest\(point)")
pointOfInterestView.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
let animation = UIViewPropertyAnimator(duration: 0.3, curve: .easeInOut) {
print("animation is called")
self.pointOfInterestView.transform = .identity
self.pointOfInterestView.alpha = 1
}
animation.startAnimation()
}
}
I put two prints, which are focus point
and point of interest
, and they show the same value, like (0.6326836581709145, 0.46399999999999997).
I thought writing pointOfInterestView.center = point
would change the position of a view to where it's tapped.
Could you point me at if I do something wrong, or where should I look to solve this issue?
When you are calculating focusPoint, you are dividing the x and y coordinates by the screen width and height. This essentially turns them into percentages (which is why you are seeing those small decimal values).
If you want the actual point (which seems to be what you use later), don’t divide by the screen size.