UIScrollView - tell the difference between setContentOffset and manual scrolling
Asked Answered
H

3

7

I have a scrollview, which automatically advances every 3 seconds to the next "page" (page control also used). However, once the user touches the scrollview, I want to stop the auto-advancing.

I have subclassed the scrollview. I can detect touchesBegan in the subclass, but touchesMoved is not called, so I can't tell if the user has manually swiped the scrollview. I can't use scrollviewDidScroll, because that gets called when I set the contentOffset when the auto-advance timer fires.

So what are my other options for detecting touches? Why isn't touchesMoved called on the scrollview subclass?

Honorable answered 15/6, 2012 at 5:40 Comment(0)
H
7

Thank you for the suggestions. They helped me stumble upon this easy solution:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.scrollTimer invalidate];
}
Honorable answered 15/6, 2012 at 15:51 Comment(0)
G
2

You may want to look into the following delegate method:

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView

Per Apple:

The scroll view calls this method at the end of its implementations of the UIScrollView and setContentOffset:animated: and scrollRectToVisible:animated: methods, but only if animations are requested.

So that delegate method is only called when programatic scrolling occurs. You could set up your autoscroll call in that method, only calling it if some BOOL is false. Setting that BOOL to true in your touch event.

Or something completely different =]

It is a useful delegate method though.

~ Good luck

Grecoroman answered 15/6, 2012 at 5:55 Comment(0)
T
1

The scrollview probably nominates a subview to receive touch input — UIKit objects are very fussy about this sort of thing and generally can't even handle forwarded events.

What you probably want to do is to key-value observe either tracking or dragging (and it sounds like you want the latter). If the relevant property changes to true then you know the user has initiated scrolling.

Total answered 15/6, 2012 at 5:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.