Ios swift 2中翻转动画的工作原理

Ios swift 2中翻转动画的工作原理,ios,uiview,uiimageview,swift2.2,Ios,Uiview,Uiimageview,Swift2.2,我有一个UIView,在这个视图中我有UIImageView。我在这个ui视图中还有一个按钮。我想做的是,当我单击此按钮时,我想制作一个翻转动画,并删除我的UIImageView,然后将另一个视图加载到此超级视图中。在我的按钮点击,甚至我做了这样的事情 func shareClick() { print("SHARE CLICK") if showingBack { UIView.transitionWithView(shareView, duration: 1

我有一个
UIView
,在这个视图中我有
UIImageView
。我在这个
ui视图中还有一个按钮。我想做的是,当我单击此按钮时,我想制作一个翻转动画,并删除我的
UIImageView
,然后将另一个视图加载到此超级视图中。在我的按钮点击,甚至我做了这样的事情

func shareClick()
{
    print("SHARE CLICK")
    if showingBack {

        UIView.transitionWithView(shareView, duration: 1.0, options: .TransitionFlipFromRight, animations: {
            self.imgVwTop.removeFromSuperview()
            }, completion: nil)

        showingBack=false

    }

    else
    {
        UIView.transitionWithView(imgVwTop, duration: 1.0, options: .TransitionFlipFromRight, animations: {
            self.shareView.removeFromSuperview()
            }, completion: nil)

        showingBack=true

    }


}

我被ebhaviour搞糊涂了,不知道该怎么做。此按钮单击事件在此处不执行任何操作。

此代码可用于

   self.debug.hidden = true
   let image = UIImage(data: data!)
   self.debug.image = image

   if (swipe.direction == UISwipeGestureRecognizerDirection.Left) {
                            UIView.transitionWithView(self.debug, duration: 1.0, options: [.TransitionFlipFromRight], animations: {
                                self.debug.hidden = false
                                }, completion: { _ in })
                        }

我在隐藏新图像后将其加载到图像中。

这段代码有效

   self.debug.hidden = true
   let image = UIImage(data: data!)
   self.debug.image = image

   if (swipe.direction == UISwipeGestureRecognizerDirection.Left) {
                            UIView.transitionWithView(self.debug, duration: 1.0, options: [.TransitionFlipFromRight], animations: {
                                self.debug.hidden = false
                                }, completion: { _ in })
                        }

在隐藏新图像后,我会将其加载到图像中。

因此看起来您的想法是正确的,我相信您已正确设置了要在某种容器中翻转的视图

我认为,如果您以编程方式实例化这两个视图(在您的例子中是UIImageView和按钮),将会有所帮助。转换时,其他视图将被卸载,因为它们被列为弱视图,因此最好动态创建它们

我制作了一个视图控制器来测试这个想法,最初currentView将从故事板中实例化,然后以编程方式创建,每次按下按钮时,它将创建一个新视图来替换另一个,执行动画并在下次按下按钮时将新视图设置为当前视图

class ViewController: UIViewController {

    @IBOutlet weak var currentView: UIView!
    @IBOutlet weak var container: UIView!
    var flipped = false

    @IBAction func buttonPressed(sender: AnyObject) {
        let new: UIView!

        if flipped {
            new = UIImageView(frame: container.bounds)
            new.backgroundColor = UIColor.redColor()
            flipped = false
        }
        else {
            new = UIButton(frame: container.bounds)
            new.backgroundColor = UIColor.blueColor()
            flipped = true
        }


        let options: UIViewAnimationOptions = [.TransitionFlipFromLeft, .AllowUserInteraction, .BeginFromCurrentState]
        UIView.transitionFromView(currentView, toView: new, duration: 0.5, options: options, completion: nil)
        self.currentView = new

    }
}

希望这有帮助:)

所以看起来你的想法是正确的,我相信你已经正确地设置了视图,你想在某种容器中翻转

我认为,如果您以编程方式实例化这两个视图(在您的例子中是UIImageView和按钮),将会有所帮助。转换时,其他视图将被卸载,因为它们被列为弱视图,因此最好动态创建它们

我制作了一个视图控制器来测试这个想法,最初currentView将从故事板中实例化,然后以编程方式创建,每次按下按钮时,它将创建一个新视图来替换另一个,执行动画并在下次按下按钮时将新视图设置为当前视图

class ViewController: UIViewController {

    @IBOutlet weak var currentView: UIView!
    @IBOutlet weak var container: UIView!
    var flipped = false

    @IBAction func buttonPressed(sender: AnyObject) {
        let new: UIView!

        if flipped {
            new = UIImageView(frame: container.bounds)
            new.backgroundColor = UIColor.redColor()
            flipped = false
        }
        else {
            new = UIButton(frame: container.bounds)
            new.backgroundColor = UIColor.blueColor()
            flipped = true
        }


        let options: UIViewAnimationOptions = [.TransitionFlipFromLeft, .AllowUserInteraction, .BeginFromCurrentState]
        UIView.transitionFromView(currentView, toView: new, duration: 0.5, options: options, completion: nil)
        self.currentView = new

    }
}
希望这有帮助:)

您可以执行以下操作(我假设
共享视图
包含图像视图和按钮):

在这里,如果使用
transitionWithView
方法,则无法删除图像视图的超级视图。您所能做的最好的事情就是用翻转后要显示的新视图替换图像视图。同样,您可以通过将图像视图添加为子视图来返回图像视图。例如:

func shareButtonAction()
{
    if (self.isFlipped! == false)
    {
       UIView.transitionWithView(shareView!, duration: 0.5, options:.TransitionFlipFromRight, animations: { () -> Void in
          // self.aImageView!.image = UIImage(named: "2.jpg")
         //hear remove the imageview add new view, say flipped view
          self.aImageView!.removeFromSuperview()
          self.shareView!.addSubview(self.flippedView!)
        }, completion: { (Bool) -> Void in
            self.isFlipped! = true
            self.shareView!.bringSubviewToFront(self.shareButton!) //button should be top of the holder view
       })
    }
    else
    {
        UIView.transitionWithView(shareView!, duration: 0.5, options:.TransitionFlipFromRight, animations: { () -> Void in
            //move back, remove flipped view and add the image view
            self.flippedView!.removeFromSuperview()
            self.shareView!.addSubview(self.aImageView!)
            }, completion: { (Bool) -> Void in
                self.isFlipped! = false
                self.shareView!.bringSubviewToFront(self.shareButton!)
        })
    }
}
您可以执行以下操作(我假设
共享视图
包含图像视图和按钮):

在这里,如果使用
transitionWithView
方法,则无法删除图像视图的超级视图。您所能做的最好的事情就是用翻转后要显示的新视图替换图像视图。同样,您可以通过将图像视图添加为子视图来返回图像视图。例如:

func shareButtonAction()
{
    if (self.isFlipped! == false)
    {
       UIView.transitionWithView(shareView!, duration: 0.5, options:.TransitionFlipFromRight, animations: { () -> Void in
          // self.aImageView!.image = UIImage(named: "2.jpg")
         //hear remove the imageview add new view, say flipped view
          self.aImageView!.removeFromSuperview()
          self.shareView!.addSubview(self.flippedView!)
        }, completion: { (Bool) -> Void in
            self.isFlipped! = true
            self.shareView!.bringSubviewToFront(self.shareButton!) //button should be top of the holder view
       })
    }
    else
    {
        UIView.transitionWithView(shareView!, duration: 0.5, options:.TransitionFlipFromRight, animations: { () -> Void in
            //move back, remove flipped view and add the image view
            self.flippedView!.removeFromSuperview()
            self.shareView!.addSubview(self.aImageView!)
            }, completion: { (Bool) -> Void in
                self.isFlipped! = false
                self.shareView!.bringSubviewToFront(self.shareButton!)
        })
    }
}