tags:

views:

64

answers:

1

I have an image in a view which contains a 9x9 grid. I want to move the object along the grid, which is made up of a column array(9) inside another array(9), using a pan movement. The image should move square to square in the grid. The code below is what I have so far. The problem is the image jumps 3 - 4 squares at a time. It is much too sensitive. Can anyone shed some light as to why and make some suggestions as to what to do to fix this sensitivity issue issue?

Code which works:

-(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  A: 

I think that below the if (canMove) { you should accumulate movements. Let me explaing, you should compute the absolute deltaX of the movement in this way:

deltaX = currentPosition.x - gestureStartPoint.x;

where deltaX is a class variable. When this value is greater than a threshold then you will do a one-block movement. Adjusting that threshold you can change sensitivity. Of course you have also to take into account the Y component.

MaxFish
I have tried this and it also moves much too fast. I believe touchesMoved is being called too often.
Oh Danny Boy
It's not a matter of how often touchMoved is called. It's only a matter o threshold, try this:if (canMove) { deltaX = (int)(currentPosition.x - gestureStartPoint.x); if (deltaX > thresholdX) { deltaX = 0; // TODO: Move one-block // Resets start point gestureStartPoint = currentPosition; NSLog(@"-------------> Block Moved"); }... YOUR CODE ...
MaxFish
This is extremely close. I think I have it from here. Thank you so much.
Oh Danny Boy