指向旋转的CGRect内部

时间:2013-06-08 18:03:11

标签: ios core-graphics cgrect

如何正确确定一个点是否在旋转的CGRect /框架内?

使用Core Graphics旋转框架。

到目前为止,我发现了一种计算点是否在三角形内部的算法,但这并不是我需要的。

正在旋转的帧是一个带有几个子视图的常规UIView。

2 个答案:

答案 0 :(得分:12)

让我们假设您使用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方法显然不需要在手势识别器中使用,而是可以在任何上下文中使用。但希望这说明了这项技术。

答案 1 :(得分:1)

使用CGRectContainsPoint()检查点是否在矩形内。

相关问题