2011-08-05 59 views
5

我想要做的是将手指在屏幕上移动(touchesMoved)并沿touchesMoved生成的点绘制均匀间隔的图像(可能是CGImageRefs)。我可以画线,但我想要生成的东西看起来像这样(对于这个例子,我使用的是一个箭头的图像,但它可能是任何图像,可能是我的狗的图片:))主要的是当用iPhone或iPad上的手指进行绘图时,图像均匀分布。在iOS中沿着路径均匀地绘制图像

enter image description here

回答

2

假设您已有追踪用户的触摸,因为他们把他们的触摸屏幕上的代码,这听起来像你想,当他们搬到等于图像的长度的距离来检测,此时你想在他们的触摸下绘制另一个图像副本。

要做到这一点,我想你将需要:

  • 计算图像的长度(宽度)
  • 执行代码来绘制图像的拷贝到您的视图,旋转到任何角度
  • 每次用户的触摸移动时(例如在touchesMoved :)中:
    • 每次移动触摸时计算触摸的增量,并生成该增量的“长度”(例如像sqrt(dx^2 + dy^2))
    • 累积距上一幅图像的距离
    • 如果距离已达到图像的长度,请在触摸的当前位置下绘制图像的副本,并进行适当旋转(可能根据从最后一个图像到当前位置)

你觉得如何?

+0

让我试试吧。我从来没有想过将图像的位置放在距离上。我想我需要添加或调整路径上的点。 – cdasher

+0

正是我在找的东西,会在清理完后发布工作代码。 (你没有脱离你的头顶知道如何计算一个合适的矢量你:) :) – cdasher

+0

想出了矢量谢谢! – cdasher

8

首先是巨大的道具出去肯德尔。所以,根据他的回答,这里是取UIImage的代码,根据触摸之间的距离在屏幕上沿着路径绘制它(不是真实的路径参考,只是由点创建的逻辑路径),然后旋转图像正确地基于当前点和前一点的VECTOR。我希望你喜欢它:

首先你需要加载图像被一遍又一遍用作CGImage:

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"arrow.png" ofType:nil]; 
    UIImage *img = [UIImage imageWithContentsOfFile:imagePath]; 
    image = CGImageRetain(img.CGImage); 

请确保您的dealloc您致电

CGImageRelease(image); 

然后在touchesBegan中,将起始点存储在方法外的var中(在此标头中声明它:)在这种情况下,我将绘制到UIView中

然后在触摸开始:

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

{

UITouch *touch = [touches anyObject]; 
lastPoint = [touch locationInView:self]; 

}

终于在touchesMoved,绘制位图到屏幕上,然后当你的距离已经移动到足以(以我的情况73,因为我的图像是73像素x 73像素)将该图像绘制到屏幕上,保存新图像并将lastPoint设置为currentPoint

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

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

     double deltaX = lastPoint.x - currentPoint.x; 
     double deltaY = lastPoint.y - currentPoint.y; 

     double powX = pow(deltaX,2); 
     double powY = pow(deltaY,2); 

     double distance = sqrt(powX + powY); 
     if (distance >= 73){ 

      lastPoint = currentPoint; 

      UIGraphicsBeginImageContext(self.frame.size); 
      [drawImage.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
      CGContextSaveGState(UIGraphicsGetCurrentContext()); 

      float angle = atan2(deltaX, deltaY); 
      angle *= (M_PI/180); 


      CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(currentPoint.x, currentPoint.y, 73, 73),[self CGImageRotatedByAngle:image angle:angle * -1]); 
      CGContextRestoreGState(UIGraphicsGetCurrentContext()); 
      drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
      UIGraphicsEndImageContext(); 
      distance = 0; 

     } 

    } 


    - (CGImageRef)CGImageRotatedByAngle:(CGImageRef)imgRef angle:(CGFloat)angle 
{ 
    CGFloat angleInRadians = angle * (M_PI/180); 
    CGFloat width = CGImageGetWidth(imgRef); 
    CGFloat height = CGImageGetHeight(imgRef); 

    CGRect imgRect = CGRectMake(0, 0, width, height); 
    CGAffineTransform transform = CGAffineTransformMakeRotation(angleInRadians); 
    CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, transform); 

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef bmContext = CGBitmapContextCreate(NULL, 
                rotatedRect.size.width, 
                rotatedRect.size.height, 
                8, 
                0, 
                colorSpace, 
                kCGImageAlphaPremultipliedFirst); 
    CGContextSetAllowsAntialiasing(bmContext, FALSE); 
    CGContextSetInterpolationQuality(bmContext, kCGInterpolationNone); 
    CGColorSpaceRelease(colorSpace); 
    CGContextTranslateCTM(bmContext, 
          +(rotatedRect.size.width/2), 
          +(rotatedRect.size.height/2)); 
    CGContextRotateCTM(bmContext, angleInRadians); 
    CGContextTranslateCTM(bmContext, 
          -(rotatedRect.size.width/2), 
          -(rotatedRect.size.height/2)); 
    CGContextDrawImage(bmContext, CGRectMake(0, 0, 
              rotatedRect.size.width, 
              rotatedRect.size.height), 
         imgRef); 

    CGImageRef rotatedImage = CGBitmapContextCreateImage(bmContext); 
    CFRelease(bmContext); 
    [(id)rotatedImage autorelease]; 

    return rotatedImage; 
} 

这将创建一个看起来像这样的图像:

enter image description here

将增加以下内容(以便尽量在空隙填补一些修改上面的代码,其中touchesMoved丢失当你快速移动时有一些点:

CGPoint point1 = CGPointMake(100, 200); 

CGPoint point2 = CGPointMake(300, 100); 



double deltaX = point2.x - point1.x; 
double deltaY = point2.y - point1.y; 

double powX = pow(deltaX,2); 
double powY = pow(deltaY,2); 

double distance = sqrt(powX + powY); 

distance = 0; 


for (int j = 1; j * 73 < distance; j++) 
{ 
    double x = (point1.x + ((deltaX/distance) * 73 * j)); 
    double y = (point1.y + ((deltaY/distance) * 73 * j)); 


    NSLog(@"My new point is x: %f y :%f", x, y); 
} 
+0

要添加以下内容(对上述代码进行一些更改以尝试填充touchesMoved在快速移动时缺少一些点的空白处: – cdasher

+0

感谢您的帮助,我该如何绘制x和y点建议我 – dineshprasanna

+0

uper代码中的drawimage是什么 –