1

Im trying to flip a UIImageView to its other side to reveal whats behind it. Everything works fine the first flip over, revealing the image behind it. The issue is when i try to flip it back to hide the image. I get an error that its trying to find a nil.Thanks for any help.

import UIKit

class ViewController: UIViewController {
@IBOutlet weak var mainLeftView: UIView!
@IBOutlet weak var back: UIImageView!
@IBOutlet weak var front: UIImageView!





override func viewDidLoad() {
    super.viewDidLoad()


    let singleTap = UITapGestureRecognizer(target: self, action: Selector("tappedItem"))
    singleTap.numberOfTapsRequired = 1







    mainLeftView.addGestureRecognizer(singleTap)
     mainLeftView.userInteractionEnabled = true;
    mainLeftView.addSubview(front)

}



var showingFront = true;

func tappedItem() {


   /*     TRIED METHOD NUMBER ONE


    var views: (front: UIImageView, back: UIImageView)

    if ((self.front.superview) != nil){
        views = (front: self.front, back: self.back)
    } else{
        views = (front: self.back, back: self.front)
    }
    UIView.transitionFromView(views.front, toView: views.back, duration: 1.0, options:UIViewAnimationOptions.TransitionFlipFromRight , completion: nil) 

*/

  // METHOD NUMBER TWO


      if (showingFront){
      UIView.transitionFromView(front, toView: back, duration: 0.5, options: UIViewAnimationOptions.TransitionFlipFromRight, completion: nil)
        showingFront = false;
    }

    else{
        UIView.transitionFromView(back, toView: front, duration: 0.5, options: UIViewAnimationOptions.TransitionFlipFromLeft, completion: nil)
        showingFront = true;

    }

}
casillas
  • 16,351
  • 19
  • 115
  • 215
Antonio Lipiz
  • 82
  • 1
  • 9

2 Answers2

0

use strong rather than weak.

@IBOutlet strong var mainLeftView: UIView!
@IBOutlet strong var back: UIImageView!
@IBOutlet strong var front: UIImageView!
casillas
  • 16,351
  • 19
  • 115
  • 215
0

I accomplished something similar. I used 3 views. The first being a containerView, the other 2 being subviews within the container view.

You will need to keep track of the current state of the container view (isFlipped) to determine how to flip the container view. Once the animation is occurring simple hide the front or back view.

    if !containerView.isFlipped {
        UIView.transitionWithView(containerView, duration: 0.75, options: UIViewAnimationOptions.TransitionFlipFromLeft, animations: { () -> Void in
            frontView.hidden = true
            backView.hidden = false

            }, completion: { (completion:Bool) -> Void in
                containerView.isFlipped = true
        })
    } else {
        UIView.transitionWithView(containerView, duration: 0.75, options: UIViewAnimationOptions.TransitionFlipFromRight, animations: { () -> Void in
            frontView.hidden = false
            backView.hidden = true
            }, completion: { (completion:Bool) -> Void in
                containerView.isFlipped = false
        })
    }
Dan
  • 191
  • 1
  • 7