Kent
Kent

Reputation: 13

Animate multiple views in a subviews with delay less than 1 second

just started to use Obj C for iOS, I was trying to animate all the views (UIImageView) inside my subviews, but when I set the delay time to less than 1 second, the animation would animate all views at once. If i set the delay to 1 second or more, it will be animate correctly but the second animation will only start after the first one completes. I want to animate all those views back to back for example view number 1 would start animating, before it completes, the second should start to animate.

Here are my code.

-(void)animate
{
    int delaytime = 0.3; //first view will start after 0.3 secs

    for(UIView *view in self.subviews)
    {
        view.alpha = 0; //set it to transparent
        delaytime += 1 ; //couldn't get the desired effect when it is less than 1
        [UIView animateWithDuration:0.3
                              delay:delaytime
                            options: UIViewAnimationOptionTransitionNone
                         animations:^{
                             view.alpha = 1;       // fade in             }
                         completion:^(BOOL finished){
                             NSLog(@"Complete");
                         }]; 
    }
}

Upvotes: 1

Views: 1141

Answers (1)

David Rönnqvist
David Rönnqvist

Reputation: 56625

Your delay variable is an int and is therefore always rounded down to the integer value (0.3 becomes 0 and even 0.9999 becomes 0). This is the reason why all "delays" less than one second starts immediately for you, an int variable can't hold decimal values.

You should instead us the correct type NSTimeInterval

NSTimeInterval delaytime = 0.3;

Upvotes: 2

Related Questions