指向旋转的CGRect内部

Did*_*ids 5 core-graphics ios cgrect

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

框架随Core Graphics旋转.

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

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

Rob*_*Rob 12

让我们假设您使用transform属性来旋转视图:

self.sampleView.transform = CGAffineTransformMakeRotation(M_PI_2 / 3.0);
Run Code Online (Sandbox Code Playgroud)

如果再有一个手势识别,例如,你可以看到,如果用户使用在该位置挖掘locationInView与旋转视图,并为您的旋转自动因素:

- (void)handleTap:(UITapGestureRecognizer *)gesture
{
    CGPoint location = [gesture locationInView:self.sampleView];

    if (CGRectContainsPoint(self.sampleView.bounds, location))
        NSLog(@"Yes");
    else
        NSLog(@"No");
}
Run Code Online (Sandbox Code Playgroud)

或者您可以使用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");
}
Run Code Online (Sandbox Code Playgroud)

convertPoint方法显然不需要在手势识别器中使用,而是可以在任何上下文中使用.但希望这说明了这项技术.