3
아이폰 화면을 터치하는 방법을 찾아서 앱을 개발하고 싶습니다. 에리카 사돈 아이폰 개발자 요리 책 3.0 Code Samples의 8 장에 예제 레시피 (09- 페인트)를 발견했습니다. 이 예제에서 볼 수 있듯이 화면의 터치는 NSMutableArray
에 저장되어 나중에 UIView
메서드 drawRect
을 사용하여 그려집니다.손가락 드로잉의 효율성 문제
points
어레이가 중요한 수의 포인트를 유지할 때이 솔루션을 사용할 때 문제가 발생합니다. 이 문제를 어떻게 해결할 수 있습니까? UIView
을 지우지 않고 메서드를 호출하고 마지막 줄 (이미 페인트 된 선과 points
배열의 마지막 점 사이)을 추가하는 방법은 어떻게됩니까?
// Start new array
- (void) touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event
{
self.points = [NSMutableArray array];
CGPoint pt = [[touches anyObject] locationInView:self];
[self.points addObject:[NSValue valueWithCGPoint:pt]];
}
// Add each point to array
- (void) touchesMoved:(NSSet *) touches withEvent:(UIEvent *) event
{
CGPoint pt = [[touches anyObject] locationInView:self];
[self.points addObject:[NSValue valueWithCGPoint:pt]];
[self setNeedsDisplay];
}
// Draw all points
- (void) drawRect: (CGRect) rect
{
if (!self.points) return;
if (self.points.count < 2) return;
CGContextRef context = UIGraphicsGetCurrentContext();
[current set];
CGContextSetLineWidth(context, 4.0f);
for (int i = 0; i < (self.points.count - 1); i++)
{
CGPoint pt1 = POINT(i);
CGPoint pt2 = POINT(i+1);
CGContextMoveToPoint(context, pt1.x, pt1.y);
CGContextAddLineToPoint(context, pt2.x, pt2.y);
CGContextStrokePath(context);
}
}
감사합니다.
아무도 도와 줄 수 없나요? –