ZAFAR007 ZAFAR007 - 3 months ago 8
Objective-C Question

UIViewAnimationOptions.TransitionFlipFromLeft not show UIView second time

I am trying to Flip two

UIViews
. I've try to flip
UIView
using programmatically and it works perfect. But when i've try to flip
UIView
that i created in
storyboard
it not works, First time it flip UIView but second time it flip blank
UiViews
? Any one have any idea is there any mistake in my code?

enter image description here
In this picture Top left
Debug view Hierarchy
picture is before animating button and bottom left
Debug view Hierarchy
picture is after animating picture.

When second time i animate the
UIView
it Flip like this below picture.
enter image description here

class ViewController: UIViewController {

@IBOutlet var container: UIView!
@IBOutlet var blueSquare : UIView!
@IBOutlet var redSquare : UIView!

override func viewDidLoad() {
super.viewDidLoad()

}

@IBAction func animateButtonTapped(sender: AnyObject) {
// create a 'tuple' (a pair or more of objects assigned to a single variable)
var views : (frontView: UIView, backView: UIView)

if ((self.redSquare.superview) != nil) {
views = (frontView: self.redSquare, backView: self.blueSquare)
}
else {
views = (frontView: self.blueSquare, backView: self.redSquare)
}

// set a transition style
let transitionOptions = UIViewAnimationOptions.TransitionFlipFromLeft

// with no animation block, and a completion block set to 'nil' this makes a single line of code
UIView.transitionFromView(views.frontView, toView: views.backView, duration: 1.0, options: transitionOptions, completion: nil)

}
}



Programmatically
That code is perfectly works.


let container = UIView()
let redSquare = UIView()
let blueSquare = UIView()

override func viewDidLoad() {
super.viewDidLoad()

// set container frame and add to the screen
self.container.frame = CGRect(x: 60, y: 60, width: 200, height: 200)
self.view.addSubview(container)

// set red square frame up
// we want the blue square to have the same position as redSquare
// so lets just reuse blueSquare.frame
self.redSquare.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
self.blueSquare.frame = redSquare.frame

// set background colors
self.redSquare.backgroundColor = UIColor.redColor()
self.blueSquare.backgroundColor = UIColor.blueColor()

// for now just add the redSquare
// we'll add blueSquare as part of the transition animation
self.container.addSubview(self.redSquare)
}

@IBAction func animateButtonTapped(sender: AnyObject) {

// create a 'tuple' (a pair or more of objects assigned to a single variable)
var views : (frontView: UIView, backView: UIView)

if((self.redSquare.superview) != nil){
views = (frontView: self.redSquare, backView: self.blueSquare)
}
else {
views = (frontView: self.blueSquare, backView: self.redSquare)
}

// set a transition style
let transitionOptions = UIViewAnimationOptions.TransitionFlipFromLeft

// with no animation block, and a completion block set to 'nil' this makes a single line of code
UIView.transitionFromView(views.frontView, toView: views.backView, duration: 1.0, options: transitionOptions, completion: nil)

}



UPDATE


var check = true
@IBAction func animateButtonTapped(sender: AnyObject) {
// create a 'tuple' (a pair or more of objects assigned to a single variable)
var views : (frontView: UIView, backView: UIView)

if (check == true) {

views = (frontView: self.redSquare, backView: self.blueSquare)
check = false
}
else {
views = (frontView: self.blueSquare, backView: self.redSquare)
check = true
}

// set a transition style
let transitionOptions : UIViewAnimationOptions = [UIViewAnimationOptions.TransitionFlipFromLeft, UIViewAnimationOptions.ShowHideTransitionViews]
// with no animation block, and a completion block set to 'nil' this makes a single line of code
UIView.transitionFromView(views.frontView, toView: views.backView, duration: 1.0, options: transitionOptions, completion: nil)

}

Answer

The default behaviour for transitionFromView removes the view after animation.

let transitionOptions: UIViewAnimationOptions = [.TransitionFlipFromLeft, .ShowHideTransitionViews]

From the documentation: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/swift/struct/c:@E@UIViewAnimationOptions

  • ShowHideTransitionViews

When present, this key causes views to be hidden or shown (instead of removed or added) when performing a view transition. Both views must already be present in the parent view’s hierarchy when using this key. If this key is not present, the to-view in a transition is added to, and the from-view is removed from, the parent view’s list of subviews.