【发布时间】:2016-02-23 08:15:20
【问题描述】:
首先,我几乎检查了互联网上的每个地方,但我没有得到任何关于这个话题的解决方案。
在我的情况下,我在一个超级视图中有多个 UIView 对象,或者您可以说我正在绘制这些视图的画布。
所有这些视图都附加了平移手势,因此它们可以在其父视图的任何位置移动。
-
其中一些视图可以使用旋转手势或 CGAffineTransformRotate 进行旋转。
- 只要任何视图超出主视图,就会被删除。
下面是我的代码。
@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