Search code examples
iosswiftanimationcore-animationflip

Can't get card flip animation to work


I am working on a game that involves cards (ImageViews) with a labels over them showing a random word. I would like the intro to start with a ImageView showing the back of a card then flipping to the front with a different image and the label. I am working with the code below, but It seems to just flip to the same side. How do I incorporate a different image for the front and include the label with the word?

UIView.transitionWithView(self.imageView6, duration: 1.5, options: UIViewAnimationOptions.TransitionFlipFromRight, animations:{
},
completion: nil)

Solution

  • The views you want to transition between should be contained within another view, see the following hierarchy:

    View Hierarchy

    The transition should then be applied to the "Transition View":

    UIView.transitionWithView(self.transitionView, duration: 1.5, options: .TransitionFlipFromRight, animations:{
    
        self.frontImageView.hidden = self.showingImage;
        self.behindView.hidden = !self.showingImage;
    
    }) { (complete) -> Void in
        self.showingImage = !self.showingImage
    }
    

    self.showingImage is a simple Bool instance variable to keep track of which view is currently being shown.