翻转动画如何在swift 2中工作

时间:2016-05-16 04:47:50

标签: ios uiview uiimageview swift2.2

我有UIView,在此视图中我有UIImageView。此外,我在UIView内有一个按钮。我想要做的是当我单击此按钮时,我想制作一个翻转动画并删除我的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感到困惑,并且不明白如何去做。此按钮单击事件在此处不执行任何操作。

3 个答案:

答案 0 :(得分:4)

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

override func viewDidLoad()
{
    super.viewDidLoad()
    shareView = UIView(frame: CGRectMake(30, 100, 300, 400)) //set the frame of the holder view
    flippedView = UIView(frame: shareView!.bounds)  //setup flipped view
    flippedView!.backgroundColor = UIColor.redColor() //for test

    isFlipped = false //initially not flipped

    //set up the initial view with image and button
    aImageView = UIImageView(frame: shareView!.bounds)
    aImageView!.image = UIImage(named: "1.jpg")
    shareButton = UIButton(type: .System)
    shareButton!.setTitle("share", forState: .Normal)
    shareButton!.frame = CGRectMake(0, 0, 150, 50)
    shareButton!.addTarget(self, action: "shareButtonAction", forControlEvents: .TouchUpInside)

    //add both imageview and button to holder view
    shareView!.addSubview(aImageView!)
    shareView!.addSubview(shareButton!)

    //finally add holder to self view
    self.view.addSubview(shareView!)
}

如果您使用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!)
        })
    }
}

答案 1 :(得分:2)

所以看起来你有正确的想法,我相信你的东西设置正确,你希望在某种容器中翻转。

我认为如果你以编程方式实例化你的两个视图,在你的情况下是一个UIImageView和一个Button,它会有所帮助。当您转换时,其他视图将被卸载,因为它们被列为弱视,因此最好在运行时创建它们。

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

    }
}

希望这会有所帮助:)

答案 2 :(得分:1)

这段代码有效......

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

我隐藏它后,将新图像加载到我的图像中。