Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fade In and Fade out in Animation Swift

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
        }
like image 481
PlugInBoy Avatar asked Feb 02 '15 23:02

PlugInBoy


People also ask

What is fade in and fade out in media?

A fade-in effect begins with a solid color (Movie Maker supports both black and white) and then fades into your video. A fade-out effect begins with the video and then fades into the solid color, again either black or white.

What is the fade out effect?

In case of a video processor, the fade out effect refers to the transition form, where the video content of a certain video window smoothly dissolves to the background. This operation must be performed parallel over several monitor screens in real time.


2 Answers

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!

like image 118
AntiStrike12 Avatar answered Oct 22 '22 03:10

AntiStrike12


Starting from iOS 10 Apple launched new iOS Animations SDK that is much more powerful, especially concerning timings functions and interactivity.

Fade out code with this approach will:

UIViewPropertyAnimator(duration: 0.5, curve: .easeOut, animations: {
    self.uiImageView.alpha = 0.0
}).startAnimation()

To get more details about the Property Animator, take a look at iOS 10 Animations demo.

like image 35
Olga Konoreva Avatar answered Oct 22 '22 03:10

Olga Konoreva