【问题标题】:Using a UIImageViews coordinate bounds for another UIImageView为另一个 UIImageView 使用 UIImageViews 坐标边界
【发布时间】:2017-10-09 10:12:36
【问题描述】:

尝试设置“选定框”图形的边界,以显示用户仅在 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

【问题讨论】:

    标签: ios swift frame bounds


    【解决方案1】:

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

    let touchPoint = sender.locationInView(view)
    

    【讨论】:

      【解决方案2】:

      (代表问题作者发布)。

      其他答案的解决方案

      在用户回答下面的建议后,只需将图像对象添加为网格图像的子视图。

      sodukoGrid.addSubview(selectedBox)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-03
        • 1970-01-01
        • 1970-01-01
        • 2014-05-15
        • 2021-08-01
        • 2014-03-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多