Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollView - tell the difference between setContentOffset and manual scrolling

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?

like image 705
soleil Avatar asked Jun 15 '12 05:06

soleil


People also ask

How can I tell when a UIScrollView has finished scrolling?

scrollViewDidScroll only notifies you that the scroll view did scroll not that it has finished scrolling. The other method scrollViewDidEndScrollingAnimation only seems to fire if you programmatically move the scroll view not if the user scrolls.

What is scroll view in Swift?

Overview. UIScrollView is the superclass of several UIKit classes, including UITableView and UITextView . A scroll view is a view with an origin that's adjustable over the content view. It clips the content to its frame, which generally (but not necessarily) coincides with that of the app's main window.

What are the UIScrollView constraints?

These constraints tell the UIScrollView the boundaries of your content (sets the contentSize of the UIScrollView ). You usually only want your content to scroll in one direction. In my case, I want the scroll view to scroll vertically. Therefore, I need to set the width of my single content view to be the width of the scroll view.

What is UIScrollView in UIKit?

UIScrollView is the superclass of several UIKit classes, including UITableView and UITextView. A scroll view is a view with an origin that’s adjustable over the content view. It clips the content to its frame, which generally (but not necessarily) coincides with that of the application’s main window.

What is a scroll view?

A scroll view is a view with an origin that’s adjustable over the content view. It clips the content to its frame, which generally (but not necessarily) coincides with that of the application’s main window.

What is the Scroll View in uitextfield?

The scroll view allows us to scroll the content into view. We first need to keep track of which text field is currently being edited. You can do this in many different ways, but I chose to add the view controller as a delegate to the UITextField s.


3 Answers

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

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.scrollTimer invalidate];
}
like image 120
soleil Avatar answered Nov 07 '22 06:11

soleil


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

like image 43
Ryan Crews Avatar answered Nov 07 '22 06:11

Ryan Crews


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.

like image 41
Tommy Avatar answered Nov 07 '22 08:11

Tommy