如何在不创建新CGPath的情况下移动CGPath

Osc*_*mez 3 iphone core-graphics cgpath

我正在创建一个CGPath在我的游戏中定义一个区域,如下所示:

CGPathMoveToPoint   ( myPath, NULL, center.x, center.y );
CGPathAddLineToPoint( myPath, NULL,center.x + 100, center.y);
CGPathAddLineToPoint( myPath, NULL, center.x + 100, center.y + 100);
CGPathAddLineToPoint( myPath, NULL, center.x,  center.y + 100);
CGPathCloseSubpath  ( myPath );
Run Code Online (Sandbox Code Playgroud)

我知道这只是一个正方形,我可以使用另一个,CGRect但我希望实际创建的路径实际上并不是一个矩形(我现在只是测试).然后简单地用以下方法检测触摸区域:

if (CGPathContainsPoint(myPath, nil, location, YES))
Run Code Online (Sandbox Code Playgroud)

这一切都很好,问题是CGPath可能每秒最多移动40次.如何在不创建新的情况下移动它?我知道我可以做这样的事情来"移动"它:

center.y += x;
CGPathRelease(myPath);
myPath = CGPathCreateMutable();
CGPathMoveToPoint   ( myPath, NULL, center.x, center.y );
CGPathAddLineToPoint( myPath, NULL,center.x + 100, center.y);
CGPathAddLineToPoint( myPath, NULL, center.x + 100, center.y + 100);
CGPathAddLineToPoint( myPath, NULL, center.x,  center.y + 100);
CGPathCloseSubpath  ( myPath );
Run Code Online (Sandbox Code Playgroud)

但我必须释放并创建一条新的路径,每秒最多40次,我认为可能会有性能损失; 这是真的?

我希望能够移动它就像我正在移动一些CGRects只需将原点设置为不同的值,这可能CGPath吗?

谢谢.

编辑:我忘了提到我没有GraphicsContext,因为我没有绘制UIView.

ken*_*ytm 5

将变换应用于CGPath并针对点进行测试,相当于将变换应用于该点.

因此,你可以使用

CGPoint adjusted_point = CGPointMake(location.x - center.x, location.y - center.y);
if (CGPathContainsPoint(myPath, NULL, adjusted_point, YES)) 
Run Code Online (Sandbox Code Playgroud)

但是CGPathContainsPoint已经有一个CGAffineTransform参数(你已经有了NULL它),所以你也可以使用

CGAffineTransform transf = CGAffineTransformMakeTranslation(-center.x, -center.y);
if (CGPathContainsPoint(myPath, &transf, location, YES)) 
Run Code Online (Sandbox Code Playgroud)

如果您正在绘制,而不是更改路径,则可以直接更改绘图代码中的CTM.

CGContextSaveGState(c);
CGContextTranslateCTM(c, center.x, center.y);
// draw your path
CGContextRestoreGState(c);
Run Code Online (Sandbox Code Playgroud)

CAShapeLayer如果您需要表现,请使用a .