1

I am trying to animate a few circles and changing their opacities simultaneously.

        UIView.animateKeyframesWithDuration(2, delay: 0, options: .Repeat, animations: {
        UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0.5, animations: {
            self.innerRingView.alpha = 1
        })
        UIView.addKeyframeWithRelativeStartTime(0.5, relativeDuration: 0.5, animations: {
            self.innerRingView.alpha = 0
            self.middleRingView.alpha = 1
        })
        UIView.addKeyframeWithRelativeStartTime(1, relativeDuration: 0.5, animations: {
            self.middleRingView.alpha = 0
            self.outterRingView.alpha = 1
        })
        UIView.addKeyframeWithRelativeStartTime(1.5, relativeDuration: 0.5, animations: {
            self.outterRingView.alpha = 0
        })
        }, completion: nil)

For some reason, it never gets to the second animation:

UIView.addKeyframeWithRelativeStartTime(1, relativeDuration: 0.5, animations: {
        self.middleRingView.alpha = 0
        self.outterRingView.alpha = 1
    })

Because of this, my animation isn't work. innerRingView never goes back to 0 opacity and outterRingView never appears (all the views are set to 0 alpha by default).

What seems to be the problem here?

1 Answer 1

3

Issue appeared to be with your key frame relative start time

UIView.addKeyframeWithRelativeStartTime

which must be be in the range 0 to 1, where 0 represents the start of the overall animation and 1 represents the end of the overall animation. For example, for an animation that is two seconds in duration, specifying a start time of 0.5 causes the animations to begin executing one second after the start of the overall animation.

Sign up to request clarification or add additional context in comments.

4 Comments

Idindu beat me to it. (Voted.) Relative start times must be in the range 0 (beginning of the entire animation) and 1 (the very end of the animation)
So if I want the whole animation to be 2 seconds long. Should my keyframe relative start times be a ratio of 2? So 0, 0.25, 0.5, 0.75?
The time taken by each animation block is relative to the whole duration and you need to ensure that the start times are correct by tracking cumulative time. Your ratio, AFAIK, doesn't need to be a ratio of 2 but within whole duration of 2, the actual animation interpolates between them which is guided by the options parameter.
You can define 0, 0.25, 0.5, 0.75 but you need to accommodate your keyframe's relativeDuration accordingly. 0.25 would be better for your example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.