views:

54

answers:

1

I want my UIImageView to detect both Tap and Swipe gesture. I've coded the touchesBegan method for Swipe detection, I just want to knw how can i make my ImageView detect both the gestures.

how to code the touchesBegan so that it can handle both of them??

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if ([touches count] != 1)
    return;

floatSwipeStartX = [[touches anyObject] locationInView:self.view].x;
floatSwipeStartY = [[touches anyObject] locationInView:self.view].y;

intSwipeDirection = 0;

isSwiping = YES;

viewUp.hidden = NO;
viewLeft.hidden = NO;
viewCurrent.hidden = NO;
viewRight.hidden = NO;
viewDown.hidden = NO;

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (!isSwiping || [touches count] != 1){
    return;
}
CGFloat swipeDistanceX = [[touches anyObject] locationInView:self.view].x - floatSwipeStartX;
CGFloat swipeDistanceY = [[touches anyObject] locationInView:self.view].y - floatSwipeStartY;

CGSize contentSize = [self setContentSize];

if (! intSwipeDirection) {

    if (abs(swipeDistanceX) > abs(swipeDistanceY)) { // swipe left or right
        intSwipeDirection = SWIPE_DIRECTION_HORIZONTAL;
    } else {
        intSwipeDirection = SWIPE_DIRECTION_VERTICAL;
    }

}

if (intSwipeDirection == SWIPE_DIRECTION_HORIZONTAL) {
    viewLeft.frame = CGRectMake(swipeDistanceX - contentSize.width, 0.0f, contentSize.width, contentSize.height);
    viewCurrent.frame = CGRectMake(swipeDistanceX, 0.0f, contentSize.width, contentSize.height);
    viewRight.frame = CGRectMake(swipeDistanceX + contentSize.width, 0.0f, contentSize.width, contentSize.height);
} else {
    viewUp.frame = CGRectMake(0.0f, swipeDistanceY - contentSize.height, contentSize.width, contentSize.height);
    viewCurrent.frame = CGRectMake(0.0f, swipeDistanceY, contentSize.width, contentSize.height);
    viewDown.frame = CGRectMake(0.0f, swipeDistanceY + contentSize.height, contentSize.width, contentSize.height);
}

}

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

CGFloat swipeDistanceX = [[touches anyObject] locationInView:self.view].x - floatSwipeStartX;
CGFloat swipeDistanceY = [[touches anyObject] locationInView:self.view].y - floatSwipeStartY;

if (! isSwiping || (swipeDistanceX == 0 && swipeDistanceY == 0)) {

    //[self updateViews];

    return;
}

if (intSwipeDirection == SWIPE_DIRECTION_HORIZONTAL) {

    if (swipeDistanceX > 50.0f) {
        intNewDirection = SWIPE_TO_THE_RIGHT;
        intCurrentView = intCurrentLeft;
    } else if (swipeDistanceX < -50.0f) {
        intNewDirection = SWIPE_TO_THE_LEFT;
        intCurrentView = intCurrentRight;
    }

} else { // vertical


    if (swipeDistanceY > 50.0f) {
        intNewDirection = SWIPE_TO_THE_UP;
        intCurrentView = intCurrentUp;
    } else if (swipeDistanceY < -50.0f) {
        intNewDirection = SWIPE_TO_THE_DOWN;
        intCurrentView = intCurrentDown;
    }

}

[self updateViews];

isSwiping = NO;

}

Please guide me where to insert the code for Tap gesture. I'm using simulator 3.2

+3  A: 

If you're using sdk 3.2 or higher, this is dead easy with the UIGestureRecognizer class.

Otherwise, I'd do something along the lines of checking the amount of movement of the finger between touches began and touches ended, calling the gesture a tap if the movement is minimal and a swipe if it's large.

johnw188