Swift Infinite Fade in and Out Loop

How can I make an effect in Swift look like this: enter image description here

I want the animation to loop forever.

+6
source share
5 answers

For iOS

The UIViewAnimationOptions set provides various convenient options for achieving a combination of beautiful and complex animations. For your specific scenario, you will need two options.

UIViewAnimationOptions.Repeat
UIViewAnimationOptions.AutoReverse

Check out the code below for implementation.

The 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 created a view with a specific frame for a demo purpose.

- UIView.animateWithDuration. , [UIViewAnimationOptions.AutoReverse, UIViewAnimationOptions.Repeat] options.

, , . https://s3.amazonaws.com/uploads.hipchat.com/37040/1764070/6Iow7n7WiWf6Naz/autoReverse.gif

, UIViewAnimationOptions.AutoReverse options. ​​. https://s3.amazonaws.com/uploads.hipchat.com/37040/1764070/8fyRUlzqNHSQI47/noreverse.gif

+8

iOS

let viewSquare .

UIView.animate(withDuration: 0.5, delay: 0, options: [.repeat,.autoreverse], animations: {
            viewSquare.alpha = 0.0
        }, completion: nil)
+7

, iOS.

duration, , :

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

, CABasicAnimation CABasicAnimation :

First create a convenient UIView extension:

extension UIView {

    enum AnimationKeyPath: String {
        case opacity = "opacity"
    }

    func flash(animation: AnimationKeyPath ,withDuration duration: TimeInterval = 0.5, repeatCount: Float = 5){
        let flash = CABasicAnimation(keyPath:.opacity.rawValue)
        flash.duration = duration
        flash.fromValue = 1 // alpha
        flash.toValue = 0 // alpha
        flash.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
        flash.autoreverses = true
        flash.repeatCount = repeatCount

        layer.add(flash, forKey: nil)
    }
}

How to use it:

    // You can use it with all kind of UIViews e.g. UIButton, UILabel, UIImage, UIImageView, ...
    imageView.flash(animation: .opacity, withDuration: 1, repeatCount: 5)
    titleLabel.flash(animation: .opacity, withDuration: 1, repeatCount: 5)
0
source

Swift 5

It worked well for me. I wanted to animate the continuous appearance and disappearance of the label that I placed inside the UIView "cardHeaderView".

@IBOutlet weak var cardHeaderView: UIView!

Put it in "viewDidAppear". I went with zero delay so the animation started 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)

}
0
source

All Articles