Swift Infinite Fade in and Out Loop

2019-04-09 00:57发布

问题:

How can I make a effect in swift similar to this:

I want the animation to loop forever.

回答1:

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



回答2:

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)


回答3:

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


回答4:

If you want repeatable fade animation you can do that by using CABasicAnimation like below :

First create handy 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)