user1960169 user1960169 - 1 month ago 16
iOS Question

How the flip animation works in swift 2

I have a

UIView
, inside this view I have
UIImageView
. Also I have a button inside this
UIView
. What I want to do is when I click this button I want to make a flip animation and remove my
UIImageView
and load another view into this super view. In my button click even I did something like this

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

}


}


I'm confused with the ebhaviour and don't understand exactly how to do it. This button click event doing nothing here.

Answer

You can do the following (I assume that share view contains the image view and the button):

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

Here, you can't remove the super view of the image view if you use the transitionWithView method. The best you can do is replace the image view with new view that you want to show after being flipped. Once again, you can flip back to the image view by adding it as subview. For example:

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