문제

iPhone 화면에 손가락 움직임의 경로를 저장하고 싶습니다.지금 현재 나는 단지 택시를 읽고 NSMutableArray에 cgPoint를 추가하고 있습니다.해당 배열에서 모든 CGPoint를 인쇄하려고하면 중간 점이 누락 된 방식입니다.이게 더 좋은 방법이 있습니까?우리는 전체 경로를 직접 저장할 수 있습니까?

여기에 내가 사용하는 코드는

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
 {

fingerSwiped = NO;
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self.view];
[self.myPoints addObject:[NSValue valueWithCGPoint:lastPoint]];
  }


 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
   {
fingerSwiped = YES;

UITouch *touch = [touches anyObject];   
CGPoint currentPoint = [touch locationInView:self.view];



UIGraphicsBeginImageContext(self.view.frame.size);
[slateImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), lineWidth);
//CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),0,0,0, 1.0);
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), self.drawcolor.CGColor);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
slateImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
[myPoints addObject:[NSValue valueWithCGPoint:lastPoint]];   
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

if(!fingerSwiped) 
{
    UIGraphicsBeginImageContext(self.view.frame.size);
    [slateImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), lineWidth);
    //CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),0,0,0, 1.0);
    CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), self.drawcolor.CGColor);
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    CGContextFlush(UIGraphicsGetCurrentContext());
    slateImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [myPoints addObject:[NSValue valueWithCGPoint:lastPoint]];
}
}
.

도움이 되었습니까?

해결책

You are recording every point that iOS is willing to give you.

iOS only reports touch-moved events every 16.67 milliseconds (60 times per second). There is no way (as far as I know) to get updated touch locations faster than that.

You say you are getting straight lines when you plot the touch points. This is happening because the user is moving his finger so fast that the touch moves a significant amount in 16.67 milliseconds. The touch is moving so far between updates that when you connect the points, it doesn't look like a smooth curve. Unfortunately, there is (as I said) no way to get updates faster than 60 times per second.

The only way to deal with this is to use spline interpolation to connect the reported points. Spline interpolation is a complicated subject. You can find lots of information about it using Google.

You can see an example of this in the Adobe Ideas app on an iPad. If you draw a big spiral fast and watch carefully, you can see that the line becomes smoother when you lift your finger. I believe it's doing some incremental smoothing as you draw the spiral, and when you lift your finger, it goes back and computes a better interpolation of the entire line.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top