如何在Swift中翻转imageView

时间:2014-11-12 00:33:49

标签: ios swift

当我按下按钮时,我一直试图让两个imageViews交替进行翻转动画。 imageViews和按钮都在故事板中创建。

经过几天的努力,寻找答案后,我就在这里。我变得非常接近,但此刻,一旦图像翻转它第一次工作,然后第二次翻转导致两个图像视图移动到屏幕的左上角。

我认为这可能与我如何设置容器视图有关。也许它在第一次翻转后断开连接,需要重置?

非常感谢任何帮助。

import UIKit
var flipAnimationContainer: UIView!
class flipViewController: UIViewController {

var showingBack = false


@IBOutlet weak var frontView: UIImageView!

@IBOutlet weak var backView: UIImageView!

@IBAction func flipButton(sender: AnyObject) {


    if (showingBack) {


        UIView.transitionFromView(backView, toView: frontView, duration: 1, options: UIViewAnimationOptions.TransitionFlipFromBottom, completion: nil)

        showingBack = false


    } else {


        backView.hidden = false

        UIView.transitionFromView(frontView, toView: backView, duration: 1, options: UIViewAnimationOptions.TransitionFlipFromBottom, completion: nil)

        showingBack = true

    }


}


override func viewDidLoad() {

    super.viewDidLoad()


    //  let rect = CGRectMake(self.headsView.frame.minX, self.headsView.frame.minY, self.headsView.frame.width, self.headsView.frame.height)

    animationContainer = UIView(frame: self.frontView.frame)

    animationContainer.addSubview(backView)

    animationContainer.addSubview(frontView)

    view.addSubview(flipAnimationContainer)


    backView.hidden = true

}


override func didReceiveMemoryWarning() {

    super.didReceiveMemoryWarning()

    // Dispose of any resources that can be recreated.

    }


}

2 个答案:

答案 0 :(得分:4)

在将frontView和backView添加为子视图之前。

frontView.setTranslatesAutoresizingMaskIntoConstraints(true)
backView.setTranslatesAutoresizingMaskIntoConstraints(true)

之后。在flipButton函数中。

UIView.transitionFromView(backView, toView: self.frontView, duration: 1, options: .TransitionFlipFromBottom | .ShowHideTransitionViews , completion: nil)

.ShowHideTranstionViews选项会阻止fromView替换toView

答案 1 :(得分:2)

这是我现在拥有的。它在imageViews之间进行转换,将它们保存在一个地方,但它们在屏幕上的位置错误,并且没有翻转动画:

import UIKit

var animationContainer: UIView!

class ViewController: UIViewController {

    var showingBack = false

    @IBOutlet weak var frontView: UIImageView!

    @IBOutlet weak var backView: UIImageView!

    @IBAction func flipButton(sender: AnyObject) {

        if (showingBack) {

            UIView.transitionFromView(backView, toView: self.frontView, duration: 1, options: UIViewAnimationOptions.TransitionFlipFromBottom | UIViewAnimationOptions.ShowHideTransitionViews, completion: nil)
            showingBack = false

        } else {

            backView.hidden = false
            UIView.transitionFromView(frontView, toView: self.backView, duration: 1, options: UIViewAnimationOptions.TransitionFlipFromBottom | UIViewAnimationOptions.ShowHideTransitionViews, completion: nil)
            showingBack = true



        }


    }

    override func viewDidLoad() {
        super.viewDidLoad()

        animationContainer = UIView(frame: self.frontView.frame)
        frontView.setTranslatesAutoresizingMaskIntoConstraints(true)
        backView.setTranslatesAutoresizingMaskIntoConstraints(true)
        animationContainer.addSubview(backView)
        animationContainer.addSubview(frontView)
        view.addSubview(animationContainer)

        backView.hidden = true

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}