iphone sdk: finding the direction of scrolling in UIScrollView

Posted by Alex1987 on Stack Overflow See other posts from Stack Overflow or by Alex1987
Published on 2010-03-30T08:11:23Z Indexed on 2010/03/30 8:13 UTC
Read the original article Hit count: 410

I have a UIScrollView with only horizontal scrolling allowed, and I would like to know which direction (left, right) the user scrolls. What I did was to subclass the UIScrollView and override the touchesMoved method:

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];

    UITouch *touch = [touches anyObject];
    float now = [touch locationInView:self].x;
    float before = [touch previousLocationInView:self].x;
    NSLog(@"%f %f", before, now);
    if (now > before){
        right = NO;
        NSLog(@"LEFT");
    }
    else{
        right = YES;
        NSLog(@"RIGHT");

    }

}

But this method sometimes doesn't get called at all when I move. What do you think?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about uiscrollview