Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run two animations simultaneously

I'm trying to create an animation with two views and I've encountered some unexpected behaviors while performing them.

I want to animate both views position while doing a second animation which is transitionFlipFromBottom

Here's the code:

let initialFrame = CGRect(x: xpos, y: -310, width: 300, height: 300)

let firstView = UIView()
firstView.backgroundColor = .red
firstView.frame = initialFrame

let secondView = UIView()
secondView.backgroundColor = .white
secondView.frame = initialFrame
secondView.isHidden = false


self.view.addSubview(firstView)
self.view.addSubview(secondView)


// Here I try to move the views on screen while fliping them
UIView.animate(withDuration: 1, delay: 0, options: .curveEaseOut, animations: {
    secondView.center = self.view.center
    firstView.center = self.view.center
    self.flip(firstView: firstView, secondView: secondView)
}, completion: nil)



// This function flips the views vertically while it animates the transition from the first to the second view
fileprivate func flip(firstView: UIView, secondView: UIView) {
    let transitionOptions: UIViewAnimationOptions = [.transitionFlipFromBottom, .showHideTransitionViews]
    UIView.transition(with: firstView, duration: 0.5, options: transitionOptions, animations: {
        firstView.isHidden = true
    })

    UIView.transition(with: secondView, duration: 0.5, options: transitionOptions, animations: {
        secondView.isHidden = false
    })
}

The code above fails to execute both animations at the same time.

It only works if I place the flip function call inside the completion block, after the first animation (moving frame) finishes, as the following:

UIView.animate(withDuration: 1, delay: 0, options: .curveEaseOut, animations: {
    secondView.center = self.view.center
    firstView.center = self.view.center
}, completion: {(_) in
    self.flip(firstView: dummyView, secondView: newGroupView)
})

I have even tried to use UIView.animateKeyframes but it still doesn't work.

Am I missing something here?

Thank you.

like image 450
Ivan Cantarino Avatar asked Jan 18 '18 16:01

Ivan Cantarino


People also ask

How do I start two animations at the same time?

Open the Animation Pane Select the object on the slide that you want to animate. On the Animations tab, click Animation Pane. Click Add Animation, and pick an animation effect. To apply additional animation effects to the same object, select it, click Add Animation and pick another animation effect.

Can you have 2 animation in CSS?

Setting multiple animation property valuesThe CSS animation longhand properties can accept multiple values, separated by commas. This feature can be used when you want to apply multiple animations in a single rule and set different durations, iteration counts, etc., for each of the animations.


1 Answers

A couple of things:

  1. In transition, specify .allowAnimatedContent option.

  2. Defer the animation:

    DispatchQueue.main.async {
        UIView.animate(withDuration: 1, delay: 0, options: [.curveEaseOut], animations: {
            secondView.center = CGPoint(x: self.view.bounds.midX, y: self.view.bounds.midY)
            firstView.center = CGPoint(x: self.view.bounds.midX, y: self.view.bounds.midY)
            self.flip(firstView: firstView, secondView: secondView)
        }, completion: { _ in
        })
    }
    
  3. Somewhat unrelated, you don't want:

    secondView.center = self.view.center
    

    Instead, do:

    secondView.center = CGPoint(x: self.view.bounds.midX, y: self.view.bounds.midY)
    

    You want to set secondView.center in the coordinate space of the bounds of view, not in view's superview.

like image 116
Rob Avatar answered Oct 17 '22 10:10

Rob