Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple CALayers Animation - Fill Mode

I am separating the screen into small tiles, then animating each tile to perform a transition:

    for (int x=0; x<number_of_x_splits; x++) {

    for (int y=0; y<number_of_y_splits; y++) {

        CGSize splitSize = CGSizeMake(screenBounds.width / number_of_x_splits, screenBounds.height / number_of_y_splits);

        CATransformLayer *transformLayer = [CATransformLayer layer];
        [transformLayer setFrame:CGRectMake(splitSize.width * x, splitSize.height * y, splitSize.width, splitSize.height)];
        [transformLayer setPosition:CGPointMake((splitSize.width * x) + splitSize.width / 2, (splitSize.height * y) + splitSize.height / 2)];

        ... adding some sublayers to transformLayer...

        CABasicAnimation *rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
        [rotate setDuration:5.0];
        [rotate setFromValue:[NSNumber numberWithFloat:0]];
        [rotate setToValue:[NSNumber numberWithFloat:M_PI]];
        [rotate setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
        [rotate setTimeOffset:(1 / (number_of_x_splits + number_of_y_splits)) * (x+y)];
        [rotate setFillMode:kCAFillModeForwards];
        [rotate setRemovedOnCompletion:NO];
        [transformLayer addAnimation:rotate forKey:@"transform.rotation.y"];

    }
}

The problem is that only the last CALayer in the chain remains at final position. I have also tried to set the CALayer final transform value:

[transformLayer setTransform:CATransform3DMakeRotation(M_PI, 0, 1, 0)];

I guess it have to do with creating another instance of CALayer in the loop reset the properties of the previous layer.

Anyone have a suggestion how to remedy the situation?

like image 574
Nimrod7 Avatar asked Jul 24 '12 06:07

Nimrod7


People also ask

What is animation-fill-mode both?

The animation-fill-mode property specifies a style for the element when the animation is not playing (before it starts, after it ends, or both). CSS animations do not affect the element before the first keyframe is played or after the last keyframe is played. The animation-fill-mode property can override this behavior.

How many values are there for animation-fill-mode property?

animation-fill-mode takes four main values: forwards, backwards, both, and none.

What is animation duration?

The time that an animation takes to complete one cycle. This may be specified in either seconds ( s ) or milliseconds ( ms ).

How do I delay an animation in CSS?

The CSS animation-delay property has the following syntax: animation-delay: [time] | initial | inherit; As you can see, there are three possible values: time, initial, and inherit. The first option is [time], which is the number of seconds or milliseconds before the animation starts.


1 Answers

timeOffset is not the property you want to use, that actually changes at what point into the animation it will begin and not the delay until it starts. Instead you should set the beginTime of the animation.

Remember that the begin time should be CACurrentMediaTime() + yourDelay


The difference between timeOffset and beginTime can be illustrated like this. I know I've seen this illustration before, I just couldn't find it.

Normal animation    | 12345678 |
Begin time          |     12345678 |
Time offset         | 5678     |
like image 162
David Rönnqvist Avatar answered Oct 20 '22 13:10

David Rönnqvist