开发者

CAKeyframeAnimation with loop

开发者 https://www.devze.com 2022-12-19 20:44 出处:网络
I\'m trying to create a CAKeyframeAnimation with a little twist.I want part of the keyframe animation to loop for a while before going straight to the end.For example:

I'm trying to create a CAKeyframeAnimation with a little twist. I want part of the keyframe animation to loop for a while before going straight to the end. For example:

Intro: play keyframes 0 to 10
Loop:  play keyframes 11 to 15 (repeat for a while)
End:   play keyframes 16 to 20

CAKeyfr开发者_如何学GoameAnimation doesn't seem to provide enough flexibility, so that the only way I can think of solving this is by providing 3 distinct keyframe animations.

Is there any better solution out there?


Since no one has had a go at answering this and assuming you have not solved it yet, I thought I'd give it a bash.

Firstly I would create a property that stores the state of the animation. ie

NSUIneteger animationState; 

which stores: 0 = idleState; 1 = startState; 2 = loopedState; 3 = endState;

I would also create BOOL for determining when to stop the looped animation;

BOOL haltAnimation;

Then, initialise the animationState to 0. When you begin the animation, create the first CAAnimation object and make sure you set the delegate to self. Initialise haltAnimation to false;

In your animationDidStart method (as required by your delegate implementation) have something like this:

-(void)animationDidStart {
    switch(animationState) {
        case 0:
            animationState = 1;
            break;
        case 1:
            animationState = 2;
            break;
        case 2:
            if(haltAnimation)
                animationState = 3;
    }
}

Then in your animation finished delegate method, do something like this:

-(void)animationDidFinish {
    swtich(animationState) {
        case 1:
            /*apply stage 2 animation and assign delegate to self*/
            break;
        case 2:
            if(!haltAnimation) {
                /*apply stage 2 animation and assign delegate to self*/
            } else {
                /*apply stage 3 animation and assign delegate to self*/
            }
            break;
        case 3:
            animationState = 0;
            break;
    }
}

Then what happens is, stage 2 animation will continue for an arbitrary length until you set haltAnimation to true.

Note that this is all untested and off the cuff. I invite anyone else to offer a better design pattern.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号