我试图在屏幕上拖动UIView进入另一个UIView 我正在比较他们的帧,拖放以检查是否(bead1匹配帧中的hole1) 我可以拖放得很好,但问题是当我拖动(bead2到孔2)bead1自动被放置在hole1中(就像代码中的第二个IF变为真) 请帮助这里的功能:
func draggingView(_ sender: UIPanGestureRecognizer){
let point = sender.location(in: view)
let draggingView = sender.view!
draggingView.center = point
print(point)
if sender.state == .ended && bead1.frame.midX !=
hole1.frame.midX {
print("false")
bead1.center = CGPoint (x: view.frame.width / 2 - 25, y:
view.frame.height / 2)
}
if sender.state == .ended && bead1.frame.midX ==
hole1.frame.midX {
bead1.center = hole1.center
imageToShow.isHidden = false
// sender.isEnabled = false
print("True")
}
if sender.state == .ended && bead2.frame.minX - 10 !=
hole2.frame.minX - 10{
print("false")
bead2.center = CGPoint (x: view.frame.width / 2 + 25, y:
view.frame.height / 2)
}
if sender.state == .ended && bead2.frame.minX - 10 == hole2.frame.midX
- 10{
bead2.center = hole2.center
imageToShow.isHidden = false
// sender.isEnabled = false
print("True")
}
}
答案 0 :(得分:0)
我终于能解决它.. 我实际上需要检查UIView是否包含另一个UIView,然后将每个UIView与另一个标签(我需要为每个UIView设置标签)进行比较,如下所示..
hole1.tag = 1
bead1.tag = 1
if hole1.tag == bead1.tag{
print("Bead1 IS IN!")
}else{
print("bead1 IS OUT!!!!")
}
然后 检查它是否包含其他中心
if bead1.frame.contains(hole1.center)