【发布时间】:2013-06-08 18:03:10
【问题描述】:
如何正确确定一个点是否在旋转的 CGRect/框架内?
框架使用 Core Graphics 旋转。
到目前为止,我已经找到了一种算法,可以计算一个点是否在三角形内,但这并不是我所需要的。
被旋转的框架是一个带有几个子视图的常规 UIView。
【问题讨论】:
-
你能解释一下“用 Core Graphics 旋转”吗?
标签: ios core-graphics cgrect
如何正确确定一个点是否在旋转的 CGRect/框架内?
框架使用 Core Graphics 旋转。
到目前为止,我已经找到了一种算法,可以计算一个点是否在三角形内,但这并不是我所需要的。
被旋转的框架是一个带有几个子视图的常规 UIView。
【问题讨论】:
标签: ios core-graphics cgrect
假设您使用transform 属性来旋转视图:
self.sampleView.transform = CGAffineTransformMakeRotation(M_PI_2 / 3.0);
如果您有一个手势识别器,例如,您可以使用 locationInView 和旋转视图查看用户是否在该位置点击,它会自动为您考虑旋转:
- (void)handleTap:(UITapGestureRecognizer *)gesture
{
CGPoint location = [gesture locationInView:self.sampleView];
if (CGRectContainsPoint(self.sampleView.bounds, location))
NSLog(@"Yes");
else
NSLog(@"No");
}
或者你可以使用convertPoint:
- (void)handleTap:(UITapGestureRecognizer *)gesture
{
CGPoint locationInMainView = [gesture locationInView:self.view];
CGPoint locationInSampleView = [self.sampleView convertPoint:locationInMainView fromView:self.view];
if (CGRectContainsPoint(self.sampleView.bounds, locationInSampleView))
NSLog(@"Yes");
else
NSLog(@"No");
}
convertPoint 方法显然不需要在手势识别器中使用,而是可以在任何上下文中使用。但希望这能说明这项技术。
【讨论】:
使用CGRectContainsPoint()检查点是否在矩形内。
【讨论】: