试图翻转UIImage视图但是当翻转时我收到了一个零

时间:2015-04-16 16:09:40

标签: ios swift imageview

我试图将UIImageView翻转到另一边,以揭示其背后的内容。第一次翻转时一切正常,露出背后的图像。问题是当我试图将其翻转以隐藏图像时。我得到一个错误,它试图找到一个零。谢谢你的任何帮助。

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;

    }

}

2 个答案:

答案 0 :(得分:0)

使用强而非弱。

@IBOutlet strong var mainLeftView: UIView!
@IBOutlet strong var back: UIImageView!
@IBOutlet strong var front: UIImageView!

答案 1 :(得分:0)

我完成了类似的事情。我使用了3个视图。第一个是容器视图,另外两个是容器视图中的子视图。

您需要跟踪容器视图的当前状态(isFlipped)以确定如何翻转容器视图。动画发生后,只需隐藏前视图或后视图。

    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
        })
    }