如何在目标c中存储手指移动的路径?

slo*_*kar 1 objective-c cgpath touchesmoved cgpoint ios

我想在iPhone屏幕上存储手指移动的路径.截至目前,我只是阅读触摸并向NSMutableArray添加CGPoints.当我尝试打印该数组中的所有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]];
}
}
Run Code Online (Sandbox Code Playgroud)

rob*_*off 6

您正在记录iOS愿意为您提供的每一点.

iOS仅每16.67毫秒(每秒60次)报告触摸移动的事件.(据我所知)没有办法比这更快地获得更新的触摸位置.

你说在绘制触摸点时你会得到直线.发生这种情况是因为用户移动手指的速度非常快,以至于触摸在16.67毫秒内移动了很多.触摸在更新之间移动到目前为止,当您连接点时,它看起来不像一条平滑的曲线.不幸的是,(正如我所说)没有办法比每秒60次更快地获得更新.

解决这个问题的唯一方法是使用样条插值来连接报告的点.样条插值是一个复杂的主题.您可以使用Google找到有关它的大量信息.

您可以在iPad上的Adobe Ideas应用中查看此示例.如果您快速绘制一个大螺旋并仔细观察,您可以看到当您抬起手指时线条变得更平滑.我相信它会在绘制螺旋线时进行一些增量平滑处理,当您抬起手指时,它会向后移动并计算整条线的更好插值.