如何正确确定一个点是否位于旋转的 CGRect/框架内?
框架随 Core Graphics 一起旋转。
到目前为止,我已经找到了一种计算点是否在三角形内的算法,但这并不是我所需要的。
正在旋转的框架是一个带有一些子视图的常规 UIView。
让我们想象一下你使用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");
}
The convertPoint
方法显然不需要在手势识别器中使用,而是可以在任何上下文中使用。但希望这能说明这项技术。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)