어떻게 이동할 수 있습니 CGPath 을 만들지 않고 새로운 중 하나
-
20-09-2019 - |
문제
내가 만들기 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 );
난 이것은 그냥 스퀘어 및 수 있는 그냥 다른 사용 CGRect
하지만 경로 나를 실제로 만들지 않은 실제적으로 사각형(나는 그냥 테스트에서는 순간).다음를 검출하는 터치 지역이 단순히:
if (CGPathContainsPoint(myPath, nil, location, YES))
이 모든 작업,벌금 문제는 CGPath
이동 될 수 있습니다 최대 40 번 두 번째입니다.어떻게 이동할 수 있습니 그것을 만들 필요없이 있습니까?나는 다음과 같이 할 수 있다"이동"it:
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 );
하지만 나 놓고 새로 만들로 40 번 두 번째는 나는 생각이 있고 성능 저하를;이것이 진리입니까?
내가 좋아하는 이동 할 수 있는 것처럼 그것을 나는 현재 움직이는 일부 CGRects
단순히 설정을 기원하는 다른 값을,이 가능 CGPath
?
감사합니다.
편집:말 것도 없어요 GraphicsContext 으로 나는 그리기에 UIView
.
해결책
cgpath에 변환을 적용하고 점에 대한 테스트는 적용하는 것과 같습니다. 역 요점으로의 전환.
따라서 사용할 수 있습니다
CGPoint adjusted_point = CGPointMake(location.x - center.x, location.y - center.y);
if (CGPathContainsPoint(myPath, NULL, adjusted_point, YES))
하지만 CGPathContainsPoint
이미 a CGAffineTransform
매개 변수 (당신이 가진 NULL
-ed), 당신은 또한 사용할 수 있습니다
CGAffineTransform transf = CGAffineTransformMakeTranslation(-center.x, -center.y);
if (CGPathContainsPoint(myPath, &transf, location, YES))
그리기중인 경우 경로를 변경하는 대신 드로잉 코드에서 CTM을 직접 변경할 수 있습니다.
CGContextSaveGState(c);
CGContextTranslateCTM(c, center.x, center.y);
// draw your path
CGContextRestoreGState(c);
a CAShapeLayer
성능이 필요한 경우.
다른 팁
@KennyTM 의 대답이 완벽하게 작동한 목적의 협동,하지만 질문이 남아 있다:
어떻게 이동할 수 있습니 CGPath 을 만들지 않고 새로운 중 하나?
만,두 줄의 코드를 않았음:
UIBezierPath* path = [UIBezierPath bezierPathWithCGPath:cgPath];
[path applyTransform:CGAffineTransformMakeTranslation(2.f, 0.f)];