尝试设置“选定框”图形的边界的多种组合已消失,该图形仅显示用户在UIImageView中触摸的位置。它仍然继续坚持超级视图坐标系

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

最佳答案

设置selectedBox为sodukoGrid的子视图,或将点击位置更改为相对于全局视图。

let touchPoint = sender.locationInView(view)

10-08 12:18