Search code examples
iosswiftframebounds

Using a UIImageViews coordinate bounds for another UIImageView


Gone through so many combinations of trying to set the bounds of a 'Selected Box' graphic that is to display where the user touches within a UIImageView only. It still continues to stick the the super views coordinate system

class ViewController: UIViewController {
@IBOutlet weak var selectedBox: UIImageView!
@IBOutlet weak var sodukoGrid: UIImageView!


override func viewDidLoad() {
    super.viewDidLoad()
    selectedBox.hidden = true

    selectedBox.frame.origin.x = sodukoGrid.bounds.minX
    selectedBox.frame.origin.y = sodukoGrid.bounds.minY


    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("tapAction:"))

    self.sodukoGrid.userInteractionEnabled = true
    self.sodukoGrid.addGestureRecognizer(tapGestureRecognizer)

}

func tapAction(sender: UITapGestureRecognizer) {

    let touchPoint = sender.locationInView(self.sodukoGrid)

    print(touchPoint)

    selectedBox.center.x = CGFloat(touchPoint.x)
    selectedBox.center.y = CGFloat(touchPoint.y)
}

Image showing the selected box graphic still sticking to the superviews bounds


Solution

  • Make selectedBox sodukoGrid's subview or change your tap location to relative to global view.

    let touchPoint = sender.locationInView(view)