2011-10-21 71 views
0

我想創建代碼繪製用戶用他的手指畫。我用這個下面的代碼:繪圖touchesMoved

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    if ([touch tapCount] == 2) { 
    [myImage setHidden:YES]; 
} 
CGPoint currentTouch = [touch locationInView:self.view]; 
if (currentTouch.x >10 && currentTouch.x < 300 && currentTouch.y >245 && currentTouch.y < 440) { 
CGRect myImageRect = CGRectMake(currentTouch.x, currentTouch.y, 5.0f, 5.0f); 
myImage = [[UIImageView alloc] initWithFrame:myImageRect]; 
[myImage setImage:[UIImage imageNamed:@"dot.png"]]; 
[self.view addSubview:myImage]; 
} 

但問題是,touchesMoved不會被調用每一個像素,所以每一個點和下之間有很大的差距。所以我需要以某種方式填補這些空白。有人可以幫我用一些代碼做到這一點嗎?

在此先感謝。

回答

1

無需回答了,我回答我自己的問題:對

誰想要的答案,不過,這裏的代碼:

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

if (currentTouch.x >10 && currentTouch.x < 300 && currentTouch.y >245 && currentTouch.y < 440) { 
CGRect myImageRect = CGRectMake(currentTouch.x, currentTouch.y, 5.0f, 5.0f); 
myImage = [[UIImageView alloc] initWithFrame:myImageRect]; 
[myImage setImage:[UIImage imageNamed:@"dot.png"]]; 
    myImage.tag = tag; 
[self.view addSubview:myImage]; 
[myImage release]; 
    if (!CGPointEqualToPoint(lastTouch, CGPointMake(0, 0))) { 
     CGPoint nextPoint; 
      for (double h = 0.0; h<25.0; h++) { 
      double blend = h/25; 
      nextPoint.x = currentTouch.x + (blend * (lastTouch.x - currentTouch.x)); 
      nextPoint.y = currentTouch.y + (blend * (lastTouch.y - currentTouch.y)); 
      myImageRect = CGRectMake(nextPoint.x, nextPoint.y, 5.0f, 5.0f); 
      myImage = [[UIImageView alloc] initWithFrame:myImageRect]; 
      [myImage setImage:[UIImage imageNamed:@"dot.png"]]; 
       myImage.tag = tag; 
      [self.view addSubview:myImage]; 
      [myImage release]; 
    } 

    } 
lastTouch = currentTouch; 
} 
} 

我加了點所謂的最後觸摸到記錄最後一個點,然後(for)循環填充當前點和最後一個點之間的空格。

+0

那麼你如何使用這個,但留下一些差距?這段代碼很好用,但我不希望那些貼在一起的圖片。 –