Ios – Animation inside a UIScrollView

animationiosiphoneuiscrollview

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) { }];
}

Best Answer

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.

Related Topic