CGRectIntersection 在 CGAffineTransformRotate 之后不起作用

CGRectIntersection is not working after CGAffineTransformRotate

首先,我几乎检查了互联网上的每个地方,但我没有得到关于这个主题的任何解决方案。

在我的例子中,我在一个超级视图中有多个 UIView 对象,或者你可以说 canvas 我正在绘制这个视图的地方。

  1. 所有这些视图都附有平移手势,因此它们可以在其父视图的任何位置移动。

  2. 部分视图可以使用旋转手势或 CGAffineTransformRotate 进行旋转。

    1. 只要任何视图位于主视图之外,它就会被删除。

下面是我的代码。

@IBOutlet weak var mainView: UIView!

  var newViewToAdd = UIView()
  newViewToAdd.layer.masksToBounds = true
  var transForm = CGAffineTransformIdentity
  transForm = CGAffineTransformScale(transForm, 0.8, 1)
  transForm = CGAffineTransformRotate(transForm, CGFloat(M_PI_4)) //Making the transformation
  newViewToAdd.layer.shouldRasterize = true //Making the view edges smooth after applying transforamtion
  newViewToAdd.transform = transForm
  self.mainView.addSubview(newViewToAdd) //Adding the view to the main view.

现在,如果手势识别器位于自定义 UIView 中 Class -

var lastLocation: CGPoint = CGPointMake(0, 0)

 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    self.superview?.bringSubviewToFront(self)
    lastLocation = self.center //Getting the last center point of the view on first touch.
  }

  func detectPan(recognizer: UIPanGestureRecognizer){
    let translation = recognizer.translationInView(self.superview!) //Making the translation
    self.center = CGPointMake(lastLocation.x + translation.x, lastLocation.y + translation.y) //Updating the center point.
    switch(recognizer.state){
    case .Began:
             break
    case .Changed:
        //MARK: - Checking The view is outside of the Superview or not
        if (!CGRectEqualToRect(CGRectIntersection(self.superview!.bounds, self.frame), self.frame)) //if its true then view background color will be changed else background will be replaced.
        {
            self.backgroundColor = outsideTheViewColor
            var imageViewBin : UIImageView
            imageViewBin  = UIImageView(frame:CGRectMake(0, 0, 20, 25));
            imageViewBin.image = UIImage(named:"GarbageBin")
            imageViewBin.center = CGPointMake(self.frame.width/2, self.frame.height/2)
            addSubview(imageViewBin)

        }else{
            for subViews in self.subviews{
                if subViews.isKindOfClass(UIImageView){
                    subViews.removeFromSuperview()
                }
                self.backgroundColor = deSelectedColorForTable
            }
        }
    case .Ended:
        if (!CGRectEqualToRect(CGRectIntersection(self.superview!.bounds, self.frame), self.frame)) //If its true then the view will be deleted.
        {
            self.removeFromSuperview()
        }

    default: break
    }
}

主要问题是,如果视图未旋转或变换,则 .Changed/.Ended 案例中的所有 "CGRectIntersection" 都按预期正常工作,但如果视图被旋转或变换,则 "CGRectIntersection" 即使视图位于 "mainView" 内并从 mainview/superview.

中移除,它也始终变为 true

请帮我纠正错误。

提前致谢。

视图的框架在应用变换后得到更新。以下代码确保它在其父视图范围内。

if (CGRectContainsRect(self.superview!.bounds, self.frame))
{
    //view is inside of the Superview
}
else
{
    //view is outside of the Superview
}