对另一个 UIImageView 使用 UIImageViews 坐标边界

Using a UIImageViews coordinate bounds for another UIImageView

经历了很多尝试设置 'Selected Box' 图形边界的组合,该图形仅显示用户在 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)

(代题作者发帖).

其他答案的解决方案

根据下面用户回答的建议,简单地将图像对象添加为网格图像的子视图。

sodukoGrid.addSubview(selectedBox)