使用 UIPanGuesture 将 UIView 拖放到另一个 UIView

Drag UIView and drop to another UIView using UIPanGuesture

我想拖动顶部的红色圆圈 UIView 并放在底部的黑色圆圈 UIView 上。 当黑色圆圈至少占 50% 时,我想删除这个红色圆圈。 但此方法只有在完美定位黑圈时才会执行。 当它处于黑色区域时,请给我一些解决方案。 下面是我的代码

  if sourceCircleView.frame.contains(destinationCircleView.frame) {
        destinationCircleView.backgroundColor = UIColor.green
        sourceCircleView.isHidden = true
    }
    if sourceCircleView.frame.contains(destinationSquareView.frame) {
        sourceCircleView.backgroundColor = UIColor.red
        UIView.animate(withDuration: 2, animations: {
            self.sourceCircleView.center = self.sourceCircleViewCenter
        }, completion: nil)
    }

你可以试试

if sourceCircleView.frame.intersects(destinationCircleView.frame) {

  //
}

// 或者

let rect = sourceCircleView.frame.intersection(destinationCircleView.frame)

// here compare width of rect with destinationCircleView.frame for 50% check

 if rect.width >= ( destinationCircleView.frame.width / 2 )  && rect.height >= ( destinationCircleView.frame.height / 2 ) {
   // intersection more than 50%
 }