2010-10-01 83 views
2

移動圖像我有一個圖像在視圖中包含9x9網格。我想使用平移運動將對象沿着由另一個數組(9)內的列數組(9)組成的網格移動。圖像應該在網格中正方形移動。下面的代碼是我到目前爲止。問題是圖像每次跳躍3-4個方塊。它太敏感了。任何人都可以闡明爲什麼,並提出一些建議,以解決此問題的靈敏度問題該怎麼做?iPhone:泛移動使用touchesBegan,touchesMoved和touchesEnd

其中工程

代碼:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    gestureStartPoint = [touch locationInView:self.view]; 

    // Do not move if touch is off the grid 
    if (gestureStartPoint.x < 0 || 
     gestureStartPoint.x > 450 || 
     gestureStartPoint.y < 0 || 
     gestureStartPoint.y > 450) { 
     canMove = NO; 
    }else { 
     canMove = YES; 
    } 
} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentPosition = [touch locationInView:self.view]; 

    double thresholdX = 50; 

    if (canMove) { 
     deltaX = (int)(currentPosition.x - gestureStartPoint.x); 
     if (deltaX > thresholdX) { 
      deltaX = 0; 
      if([self blockCanMoveXDir:1]){ 
       [cBlock movX:1]; 
      } 
      // Resets start point 
      gestureStartPoint = currentPosition; 
      NSLog(@"-------------> Block Moved"); 
     } else if(deltaX < 0 && fabs(deltaX) > thresholdX) { 
      deltaX = 0; 
      if([self blockCanMoveXDir:-1]){ 
       [cBlock movX:-1]; 
      } 
      gestureStartPoint = currentPosition; 
     } 
    } 
} 

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { 
    canMove = NO; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
    canMove = NO; 
} 

回答

1

我認爲if (canMove) {下面你應該積累的運動。讓我來解釋一下,你應該這樣計算機芯的絕對deltaX:

deltaX = currentPosition.x - gestureStartPoint.x; 

其中deltaX是一個類變量。當這個值大於一個閾值,那麼你會做一個塊的運動。調整該閾值可以改變靈敏度。 當然你也要考慮Y分量。

+0

我試過這個,它也移動得太快了。我認爲touchesMoved過於頻繁地被調用。 – 2010-10-01 19:44:51

+0

這不是touchMoved被調用的頻率的問題。這只是一個閾值,試試這個: if(canMove){delta} =如果(deltaX>閾值X){ deltaX = 0; // TODO:移動一個塊 //重置起始點 gestureStartPoint = currentPosition; NSLog(@「-------------> Block Moved」); } ...您的代碼... – MaxFish 2010-10-01 20:25:01

+0

這是非常接近。我想我從這裏得到它。非常感謝。 – 2010-10-01 20:42:58

相關問題