gpt4 book ai didi

ios - CGAffineTransformRotate 之后 CGRectIntersection 不起作用

转载 作者:行者123 更新时间:2023-11-29 01:12:37 29 4
gpt4 key购买 nike

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

在我的例子中,我在一个 super View 中有多个 UIView 对象,或者你可以说我在 Canvas 上绘制这个 View 。

  1. 所有这些 View 都附加了平移手势,因此它们可以移动到其 super View 的任何位置。

  2. 其中一些 View 可以使用旋转手势或 CGAffineTransformRotate 进行旋转。

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

现在以下是我的代码。

@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
}
}

主要问题是,如果 View 未旋转或变换,则 .Changed/.Ended 情况下的所有“CGRectIntersection”都按预期正常工作,但如果 View 旋转或变换,则“CGRectIntersection”始终变为 true 即使该 View 位于“mainView”内部,并将其从主视图/ super View 中删除。

请帮助解决我的错误。

提前致谢。

最佳答案

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

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

关于ios - CGAffineTransformRotate 之后 CGRectIntersection 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35572187/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com