Ios 如何在Swift中翻转图像视图

Ios 如何在Swift中翻转图像视图,ios,swift,Ios,Swift,当我按下一个按钮时,我一直在尝试使两个图像视图与翻转动画交替。imageview和按钮都是在故事板中创建的 经过几天的努力,我终于找到了答案。我已经非常接近了,但是现在,一旦图像第一次翻转,它就会工作,然后第二次翻转会导致两个图像视图都移动到屏幕的左上角 我认为这可能与我如何设置容器视图有关。也许它在第一次翻转后断开连接,需要重置 任何帮助都将不胜感激 import UIKit var flipAnimationContainer: UIView! class flipViewControlle

当我按下一个按钮时,我一直在尝试使两个
图像视图
与翻转动画交替。
imageview
和按钮都是在故事板中创建的

经过几天的努力,我终于找到了答案。我已经非常接近了,但是现在,一旦图像第一次翻转,它就会工作,然后第二次翻转会导致两个图像视图都移动到屏幕的左上角

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

任何帮助都将不胜感激

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.

    }


}

将前视图和后视图添加为子视图之前

frontView.setTranslatesAutoresizingMaskIntoConstraints(true)
backView.setTranslatesAutoresizingMaskIntoConstraints(true)
在那之后。在flipButton功能中

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

.showHideTransitionViews
选项将防止
fromView
toView
替换。以下是我现在拥有的。它在图像视图之间转换,将它们保留在一个位置,但它们在屏幕上的位置不正确,并且没有翻转动画:

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


}

非常感谢你的帮助。看来我太笨了,即使你给我讲清楚了,我也听不懂。imageView现在保持不变,并在两个图像之间进行转换,但没有发生翻转动画,并且它在视图中的位置不正确。我只是通过注释这两行代码来实现这一点://frontView.SetTranslatesAutoResistingGMaskintoConstraints(true)//backView.SetTranslatesAutoResistingGMaskintoConstraints(true)