How to implement efficient UI updates while scrolling UIScrollView on iOS - ios

I'm looking for a way to update UI elements while an UIScrollView is scrolled. There is the function - (void)scrollViewDidScroll:(UIScrollView *)scrollView which is called multiple times per second and the method - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollViewwhich is called at the end of the scrolling (when there was a flick).
I have the feeling that doing updates in the scrollViewDidScroll is inefficient and the scrolling is not smooth anymore. What is the recommended way to do something like this?

Related

- (void)scrollViewDidScroll:(UIScrollView *)scrollView too slow in iOS 8

I have a problem where scrolling up/down and setting the contentoffset from within scrollviewdidscroll on a secondary scrollview causes a minor jittery behaviour... or more accurately a low frame rate.
I currently rely on scrollViewDidScroll to manage effects such as Parallax in my UIScrollView, these effects are applied by listening for scrollViewDidScroll, but the turn around time for each call of this method is (for some reason), too slow and causes enough of a delay for it to look kind of bad when scrolling.
Interestingly, iOS 9, runs fine.
I've tried alternative methods, such as turning off images or using AsyncDisplayKit but both have no effect on the number of times scrollViewDidScroll is fired.
It looks to me that I may need to rearchitect the way I create the parallax effect, but I'm hesitant to in case there is a quick fix.
First of all - use Xcode Instruments debug tool "Time Profiler" (Xcode menu Product->Profile, then select Time Profiler from instruments).
Don't forget to check there "Invert Call Tree" and "Hide System Libraries" checkmarks, and detect problem place in your code.
After that you can find some solution.
At least you can try to add additional check before setting contentOffset property for second scroll view:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat contentOffsetForSecondScrollView = 123.0; // Calculate second scroll view content offset
// Add additional check, if content offset doesn't change
if (self.secondScrollView.contentOffset.y != contentOffsetForSecondScrollView) {
self.secondScrollView.contentOffset = CGPointMake(0, contentOffsetForSecondScrollView);
}
}
Put all the effects in the below method.
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
Please put below delegate method to improve and detect scroling
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//Your code here
}

Parallax UIScrollView – handling two views in one scrollViewDidScroll method?

I’m creating a basic parallax effect much like the iOS7 app switcher, using two UIScrollView instances (cardScrollView and tileScrollView). I scroll one alongside the other, at a different rate, like so:
if ([scrollView isEqual:self.tileScrollView]) {
[self.cardScrollView setContentOffset:CGPointMake((self.tileScrollView.contentOffset.x + 110) * TILE_CARD_DELTA,
self.cardScrollView.contentOffset.y)];
}
This works fine when scrolling tileScrollView. However, I’d like the same to work in reverse, meaning I can scroll cardScrollView and have tileScrollView move accordingly. The issue I’m having is that calling setContentOffset actually causes cardScrollView to call scrollViewDidScroll itself, meaning they’re continually trying to set each other at the same time, and all kinds of hell break loose.
Basically, the issue here is that both scrollView instances are relying on the same scrollViewDidScroll, and so I can’t differentiate between the two of them in there.
How can I get around this one?
You are getting reference of both in this method and work as per requirement :
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView == self.tileScrollView) {
// do something
}
else {
// do something
}
}

UIScrollView scrollViewBeginDecelerating not called

In my scrollview, I have successfully used scrollViewDidEndDecelerating in my App. However, I'm adding a video player and would like to have it loaded prior to the scroll taking place. I located the delegate method scrollViewBeginDecelerating in Apple's docs, but it doesn't get called. Here is the code:
#pragma mark - UIScrollView Delegate
- (void)scrollViewBeginDecelerating:(UIScrollView *)sender
{
// setup movie view
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender
{
// previously used to display header text, etc
}
You need to implement scrollViewWillBeginDragging: and scrollViewDidScroll:. These will give you information when the user starts scrolling. Deceleration delegate calls will only start, once the user has lifted his or her finger, and there is still inertia.
The methods declared by the UIScrollViewDelegate protocol allow the adopting delegate to respond to messages from the UIScrollView class and thus respond to, and in some affect, operations such as scrolling, zooming, deceleration of scrolled content, and scrolling animations.
The scroll view’s dragging property is set to YES, and its delegate is sent the scrollViewWillBeginDragging: message
The Complete Delegate Message Sequence
When the user touches the screen the tracking sequence begins. The tracking property is set to YES immediately, and remains YES as long as the user’s finger is in contact with the screen, regardless of whether they are moving their finger.
https://developer.apple.com/library/ios/documentation/windowsviews/conceptual/UIScrollView_pg/ScrollingViewContent/ScrollingViewContent.html

scrollView.dragging == true after scrollViewDidEndDragging?

It seems that in the UIScrollViewDelegate function
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
sometimes, scrollView.dragging is true even after
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)willDecelerate
is called.
Is that somehow expected?
If willDecelerate of scrollViewDidEndDragging:willDecelerate: is YES, then yes, that's expected. You get scrollViewDidEndDragging when the user lets go of the drag, but dragging doesn't change until the visual scrolling/decelerating is done. Thus, when you let go of a drag, you may see scrollViewDidEndDragging followed by some scrollViewDidScroll (and dragging still YES).
Alternatively, you can check out tracking, which turns to NO as soon as the user lets go (even though the scroll view may still be scrolling).

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?
Thank you for the suggestions. They helped me stumble upon this easy solution:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self.scrollTimer invalidate];
}
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
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.

Resources