简体   繁体   中英

UIView.transitionWithView inside a UIView.animateKeyframesWithDuration not working as expected

I want to create some animations with a delay between them so I decided to use UIView.animateKeyframesWithDuration:delay:options:animations:completion: .

The problem is that it happens all at the same time. Here is my code:

animateKeyframesWithDuration :

UIView.animateKeyframesWithDuration(5, delay: 0, options: nil, animations: {
    UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 1/5, animations: {
        self.flipTransition(container: self.container500, view1: self.button500, view2: self.buttonDivision)
    })
    UIView.addKeyframeWithRelativeStartTime(1/5, relativeDuration: 1/5, animations: {
        self.flipTransition(container: self.container100, view1: self.button100, view2: self.buttonMultiplication)
    })
    UIView.addKeyframeWithRelativeStartTime(2/5, relativeDuration: 1/5, animations: {
        self.flipTransition(container: self.container50, view1: self.button50, view2: self.buttonSubtraction)
    })
    UIView.addKeyframeWithRelativeStartTime(3/5, relativeDuration: 1/5, animations: {
        self.flipTransition(container: self.container10, view1: self.button10, view2: self.buttonAddition)
    })
    UIView.addKeyframeWithRelativeStartTime(4/5, relativeDuration: 1/5, animations: {
        self.flipTransitionForConvertAndEqual(container: self.containerConvert, convertView: self.buttonConvert, equalView: self.buttonEqual)
    })
    }, completion: { finished in    
})

transitionWithView :

private func flipTransitionForConvertAndEqual(#container: UIView, convertView: UIView, equalView: UIView) {
    var transitionOptions: UIViewAnimationOptions!
    let duration = kAnimationDuration

    // case where equal is displayed
    if equalView.superview != nil {
        transitionOptions = .TransitionFlipFromLeft
    }
    else {
        transitionOptions = .TransitionFlipFromRight
    }

    UIView.transitionWithView(container, duration: duration, options: transitionOptions, animations: {
        self.setConvertContainer(container: container, convertView: convertView, equalView: equalView)
        }, completion: nil)
}

private func flipTransition(#container: UIView, view1: UIView, view2: UIView) {
    var transitionOptions: UIViewAnimationOptions!
    let duration = kAnimationDuration

    var views : (frontView: UIView, backView: UIView)

    if view1.superview != nil {
        views = (frontView: view1, backView: view2)
        transitionOptions = .TransitionFlipFromLeft
    }
    else {
        views = (frontView: view2, backView: view1)
        transitionOptions = .TransitionFlipFromRight
    }

    UIView.transitionWithView(container, duration: duration, options: transitionOptions, animations: {
        self.addFrontViewToContainer(container, frontView: views.backView, backView: views.frontView)
        }, completion: nil)
}

What did I do wrong? Can't figure it out.

From the documentation:

  • (void)addKeyframeWithRelativeStartTime:(double)frameStartTime relativeDuration:(double)frameDuration animations:(void (^)(void))animations NS_AVAILABLE_IOS(7_0);

// start time and duration are values between 0.0 and 1.0 specifying time and duration relative to the overall time of the keyframe animation

In your case you might want to do instaed of 1/5, 2/5, 3/5 for the relativeStartTime parameter 1 * (1/5), 2 * (1/5), 3 * (1/5) , and 1/5 for the duration, remember these are values relative to the total duration you specify for the UIView.animateKeyframesWithDuration.. method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM