【问题标题】:CGRectIntersection is not working after CGAffineTransformRotateCGRectIntersection 在 CGAffineTransformRotate 之后不起作用
【发布时间】:2016-02-23 08:15:20
【问题描述】:

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

在我的情况下,我在一个超级视图中有多个 UIView 对象,或者您可以说我正在绘制这些视图的画布。

  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 类中 -

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 中删除。

请帮助我的错误。

提前致谢。

【问题讨论】:

    标签: ios swift uiview rotation uipangesturerecognizer


    【解决方案1】:

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

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

    【讨论】:

    • 首先感谢您的宝贵时间。你能告诉我更多关于你的解决方案和我的..??? if (!CGRectEqualToRect(CGRectIntersection(self.superview!.bounds, self.frame), self.frame)) 这到底是什么问题...??
    • 您的代码对我来说也很合适。我做了一个 POC,它非常适合我。可能的原因可能是您的主视图框架超出屏幕。请检查主视图的框架和约束。
    • 好的......我也会检查一下......但问题是如果主视图的框架不在屏幕上,那么当我没有在主视图内旋转任何视图时,我的代码是如何工作的。 .顺便感谢您宝贵的时间。
    • 在我的最后一条评论中,我的意思是说,可能的原因可能是您的主视图框架不正确。尝试设置 mainView.clipsToBounds = true。
    • 感谢您的提示...如果可行,我会告诉您...谢谢。
    猜你喜欢
    • 1970-01-01
    • 2013-12-28
    • 2013-11-07
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多