如何将旋转的 UIView 保留在另一个 UIView 中? (框架不能使用)
我想在另一个视图中有一个可移动/可缩放/可旋转的视图。内部视图可以超出外部视图的框架,但我想将其一部分保留在外部视图的内部,这样内部视图就不会丢失。
我简化了这个 xcode 项目 https://github.com/nextorlg/Intersection 中的问题。
如果内部视图只是可移动和可缩放的,那么问题就已经解决了,但是当内部视图旋转时,这个解决方案就不好了,因为框架包含视图,但它不是视图本身。
对内部视图执行的每次转换我都使用此函数来验证新的视图位置,如果无效,我将恢复上次转换(这是可移动视图代码 https://github.com/nextorlg/Intersection/blob/master/intersec/MovableView.m )
-(BOOL) validInset {
CGRect outerLimit = CGRectMake(0, 0, self.superview.frame.size.width, self.superview.frame.size.height);
CGRect intersectionRect = CGRectIntersection(self.frame, outerLimit);
NSLog(@"self.frame:%f,%f,%f,%f", self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
NSLog(@"outer.frame:%f,%f,%f,%f", outerLimit.origin.x, outerLimit.origin.y, outerLimit.size.width, outerLimit.size.height);
NSLog(@"intersec.frame:%f,%f,%f,%f", intersectionRect.origin.x, intersectionRect.origin.y, intersectionRect.size.width, intersectionRect.size.height);
NSLog(@"========================");
if ( CGRectIsNull(intersectionRect) ||
intersectionRect.size.width < INSET ||
intersectionRect.size.height < INSET ) {
return NO;
}
else {
return YES;
}
}
问题是, 当第一个视图旋转一些(例如 45)度并拖动到角落时,如何确保内部视图不会丢失在外部视图后面?
一条评论,我只想保留一些外部视图内部的像素,因为内部视图可以比外部视图更大(缩放)。
我建议您下载并运行该项目以更好地理解问题,仅阅读本文很难理解它。
谢谢你!
I want to have a movable/scalable/rotable view inside another view. The inner view can go outside the outer view's frame but I want to keep part of it inside the outer one so the inner view isn't lost.
I simplified the problem in this xcode project https://github.com/nextorlg/Intersection.
If the inner view was only movable and scalable the problem would be already solved but when the inner view rotates this solution it is not good because the frame contains the view but it is not the view itself.
Every transformation performed to the inner view I use this function to validate the new view position, if it is not valid I revert the last transformation ( this is the movable view code https://github.com/nextorlg/Intersection/blob/master/intersec/MovableView.m )
-(BOOL) validInset {
CGRect outerLimit = CGRectMake(0, 0, self.superview.frame.size.width, self.superview.frame.size.height);
CGRect intersectionRect = CGRectIntersection(self.frame, outerLimit);
NSLog(@"self.frame:%f,%f,%f,%f", self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
NSLog(@"outer.frame:%f,%f,%f,%f", outerLimit.origin.x, outerLimit.origin.y, outerLimit.size.width, outerLimit.size.height);
NSLog(@"intersec.frame:%f,%f,%f,%f", intersectionRect.origin.x, intersectionRect.origin.y, intersectionRect.size.width, intersectionRect.size.height);
NSLog(@"========================");
if ( CGRectIsNull(intersectionRect) ||
intersectionRect.size.width < INSET ||
intersectionRect.size.height < INSET ) {
return NO;
}
else {
return YES;
}
}
The question is, how can I be sure the inner view it is not lost behind the outer view when the first is rotated some (45 for example) degrees and dragged to a corner?
One comment, I want to keep just some pixels inside the outer view because the inner view can be bigger (scaled) than the outer one.
I recommend you to download and run the project to understan better the problem, it is difficult to understand it just reading this.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所说,只有当内部视图不旋转时,您的方法才能正常工作。
我所知道的解决问题的最简单方法就是测试一个视图的中心或顶点是否在另一个视图的内部。
此代码将完成处理您在示例中使用的转换的工作,但它不是通用答案。
此问题的非 iPhone 相关答案可在此处找到。
您还可以阅读一些有关矩形和点交集的背景这里。
编辑:
对于可以应用于
UIView
的转换,测试顶点和中心对于大多数情况就足够了。两个众所周知的例外是:
其中:
如果您遇到其中一种情况,则应该使用另一种方法,例如 第一个链接。
As you stated, your method will only work fine when the inner view is not rotated.
The easiest method I am aware of for solving your problem is simply to test if the center or a vertex of one of the views is inside the other one.
This code will do the job for dealing with the transformations that you use in your example, but it is not a general purpose answer.
A non iphone-related answer to this question is found here.
And you can also read some background about rectangle and point intersection here.
edit:
For the transformations that can be applied to an
UIView
, to test the vertices and the center will be enough for most cases.The two well-known exceptions are:
where:
UIView
and a vertex;UIView
;UIView
.If you face one of these cases, you should use another method, like the one in the first link.