Fade In and Fade out in Animation Swift

PlugInBoy picture PlugInBoy · Feb 3, 2015 · Viewed 38.3k times · Source

I have an UIImageView with an Animation and, in the UIView, I apply a fadeIn effect, but I need to apply fade out when the UIImageView, which is animated, when is touched.

This is what I make to fade in.

UIView.animateWithDuration(0.5, delay: delay, 
    options: UIViewAnimationOptions.CurveEaseOut, animations: {
        uiImageView.alpha = 1.0
        }

Answer

AntiStrike12 picture AntiStrike12 · Feb 3, 2015

This is what I would do based on my research: (Supposing you're using storyboard)

  1. Go to your UIImageView, and under the Attributes, check the "User Interaction Enabled" checkbox.

  2. Drag a TapGestureRecognizer on top of the image view.

  3. Control click on the Tap Gesture and drag to make a action on your ViewControler.swift.

  4. Add the following code inside:

    UIView.animate(withDuration: 0.5, delay: 0.5, options: .curveEaseOut, animations: {
        self.uiImageView.alpha = 0.0
    }, completion: nil) 
    

Then you're done!