Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animation inside a UIScrollView

I want to fade-out a view as it is scrolling inside a parent UIScrollview. When the fade-out animation begins, the scroll view stops scrolling. It jumps to the correct position when the fade is complete.

My fade-out is achieved with animateWithDuration and block objects, triggered upon a page-change I detect in scrollViewWillBeginDragging.

Does anyone know how to make them both happen simultaneously? Just to be clear, I am not 'animating' the UIScrollView scrolling - rather it is happening via user interaction of swiping.

EDIT:

Here is the code I'm using to fade the UIView. This code is in a UIViewController derived class, which is the delegate for a UIScrollView. When the user starts dragging his finger, I want to fade out the subView. But when the user starts draggin a finger, the subview fades and the scrolling stops. After the subView has completely faded out, the the scroll view will then snap to the location where the user's finger is.

-(void)scrollViewWillBeginDragging:(UIScrollView*)scrollView
{
    [UIView animateWithDuration:0.5
        animations:^
        {
            self.subView.alpha = 0.0f;
        }
        completion:^(BOOL finished) { }];
}
like image 417
OpenUserX03 Avatar asked Apr 14 '11 07:04

OpenUserX03


2 Answers

A little late, but if you want to keep using blocks, you can use:

animateWithDuration:delay:options:animation:complete:

add "UIViewAnimationOptionAllowUserInteraction" to options to allow interaction while scrolling.

I'm sure that you will still have the lag problem. Here's the best way I can explain it. Please forgive me in advance since I'm probably using the wrong terms. All animations must run on the main thread. When you call an animation, iOS first *P*rocesses then it *R*enders before it generates *F*rames. It looks like this.

PPPPRRRRFFFFFFFFFFFFFFFFFF

But since ScrollViews don't know how long your animation is going to be or when it will end, it has to perform the animation like this.

PRFPRFPRFPRFPRFPRFPRFPRF

My theory is that the lag you are experiencing has to do with these two calls colliding on the main thread at the same time. I'm not sure how you would solve this problem other than with a faster chip. I've that you could push one animation to the CPU and one to the GPU, but I'm not that advanced at programming yet.

like image 155
David Chu Avatar answered Sep 24 '22 11:09

David Chu


very interesting ... I've checked this out, and yes, i have the same effect ... Well, it seems that the animateWithDuration somehow blocks the main thread ... which is not logical, and the documentation doesn't say anything about it either .. However there is an easy workaround, something similar to this: (i've set the animation duration to 3 so i can see that it's working while i'm moving my scroll view :) ...)

[UIView beginAnimations:@"FadeAnimations" context:nil];
[UIView setAnimationDuration:3]; 

self.subview.alpha = 0.0f;

[UIView commitAnimations];
like image 37
Moszi Avatar answered Sep 23 '22 11:09

Moszi