Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Infinite Fade in and Out Loop

How can I make a effect in swift similar to this: enter image description here

I want the animation to loop forever.

like image 648
Alex Avatar asked Jun 18 '16 03:06

Alex


5 Answers

For iOS

UIViewAnimationOptions set provides different handy options to achieve a combination of beautiful and complex animations. For your particular scenario you will require two of the options.

UIViewAnimationOptions.Repeat
UIViewAnimationOptions.AutoReverse

Check out the code below for implementation.

Code:

class MyViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
        view.backgroundColor = UIColor.blueColor()
        self.view.addSubview(view)

        UIView.animateWithDuration(1, 
        delay: 0, 
        options: [UIViewAnimationOptions.Autoreverse, UIViewAnimationOptions.Repeat], 
        animations: {
              view.backgroundColor = UIColor.clearColor()
          }, 
        completion: nil)
    }

}

Explanation: I have created a view with a specific frame for demo purpose.

The part you are interested in is the UIView.animateWithDuration method. Notice that I have provided an array [UIViewAnimationOptions.AutoReverse, UIViewAnimationOptions.Repeat] in the options parameter.

These two options are enough to achieve a smooth and forever looping animation like below. https://s3.amazonaws.com/uploads.hipchat.com/37040/1764070/6Iow7n7WiWf6Naz/autoReverse.gif

If you don't want to reverse the animation, just remove UIViewAnimationOptions.AutoReverse from the array in the options parameter. You will get an animation like this. https://s3.amazonaws.com/uploads.hipchat.com/37040/1764070/8fyRUlzqNHSQI47/noreverse.gif

like image 199
saugat gautam Avatar answered Nov 03 '22 18:11

saugat gautam


For iOS

let viewSquare be the name of the blue square in your question.

UIView.animate(withDuration: 0.5, delay: 0, options: [.repeat,.autoreverse], animations: {
            viewSquare.alpha = 0.0
        }, completion: nil)
like image 12
Chanchal Raj Avatar answered Nov 03 '22 20:11

Chanchal Raj


Swift 5.1

let duration = 0.5


func fadeIn(finished: Bool) {
    UIView.animate(withDuration: self.duration, delay: 0,
                   options: [.curveEaseInOut],
                   animations: { self.tftMap.alpha = 1 }, completion: self.fadeOut)
}

func fadeOut(finished: Bool) {
    UIView.animate(withDuration: self.duration, delay: 0,
                   options: [.curveEaseInOut],
                   animations: { self.tftMap.alpha = 0 }, completion: self.fadeIn)
}
like image 3
Ahmed Safadi Avatar answered Nov 03 '22 19:11

Ahmed Safadi


I assume you are programming for iOS.

Play around with the duration to see what suits you best:

class ViewController: UIViewController {
    @IBOutlet weak var myView: UIView!
    let duration = 0.5

    override func viewDidLoad() {
        super.viewDidLoad()
        self.fadeOut(true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func fadeIn(finished: Bool) {
        UIView.animateWithDuration(self.duration, delay: 0, options: [.CurveEaseInOut], animations: { self.myView.alpha = 1 } , completion: self.fadeOut)
    }

    func fadeOut(finished: Bool) {
        UIView.animateWithDuration(self.duration, delay: 0, options: [.CurveEaseInOut], animations: { self.myView.alpha = 0 } , completion: self.fadeIn)
    }
}
like image 2
Code Different Avatar answered Nov 03 '22 18:11

Code Different


Swift 5

This worked well for me. I wanted to animate a continuous fade in and out of a label, which I placed inside the "cardHeaderView" UIView.

@IBOutlet weak var cardHeaderView: UIView!

Place this inside the "viewDidAppear". I went with a delay of zero so the animation would start right away.

fadeViewInThenOut(view: cardHeaderView, delay: 0)

Here is the function.

func fadeViewInThenOut(view : UIView, delay: TimeInterval) {

    let animationDuration = 1.5

    UIView.animate(withDuration: animationDuration, delay: delay, options: [UIView.AnimationOptions.autoreverse, UIView.AnimationOptions.repeat], animations: {
        view.alpha = 0
    }, completion: nil)

}
like image 1
Bill Avatar answered Nov 03 '22 19:11

Bill