Swift Infinite淡入和淡出循环

时间:2016-06-18 03:40:32

标签: xcode swift animation swift2 infinite-loop

如何在swift中产生类似于此的效果: enter image description here

我希望动画永远循环。

6 个答案:

答案 0 :(得分:8)

适用于iOS

UIViewAnimationOptions set提供了不同的便捷选项,可实现美观和复杂动画的组合。对于您的特定情况,您将需要两个选项。

system.diagnostic.process.start("http://www.facebook.com/")

查看以下代码以便实施。

<强>代码

UIViewAnimationOptions.Repeat
UIViewAnimationOptions.AutoReverse

<强>说明: 我创建了一个带有特定框架的视图用于演示目的。

您感兴趣的部分是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) } } 方法。请注意,我在UIView.animateWithDuration参数中提供了一个数组[UIViewAnimationOptions.AutoReverse, UIViewAnimationOptions.Repeat]

这两个选项足以实现如下所示的平滑且永久循环的动画。 https://s3.amazonaws.com/uploads.hipchat.com/37040/1764070/6Iow7n7WiWf6Naz/autoReverse.gif

如果您不想反转动画,只需从options参数中的数组中删除UIViewAnimationOptions.AutoReverse即可。你会得到这样的动画。 https://s3.amazonaws.com/uploads.hipchat.com/37040/1764070/8fyRUlzqNHSQI47/noreverse.gif

答案 1 :(得分:6)

适用于iOS

viewSquare成为问题中蓝色方块的名称。

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

答案 2 :(得分:1)

我假设您正在为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)
    }
}

答案 3 :(得分:1)

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

答案 4 :(得分:0)

如果您想要可重复的淡入淡出动画,则可以使用CABasicAnimation进行如下操作:

首先创建方便的UIView扩展:

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

使用方法:

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

答案 5 :(得分:0)

快速5

这对我来说很好。我想设置一个连续的淡入淡出动画,并将其放置在“ cardHeaderView” UIView中。

@IBOutlet weak var cardHeaderView: UIView!

将其放置在“ viewDidAppear”中。我的延迟为零,所以动画会立即开始。

fadeViewInThenOut(view: cardHeaderView, delay: 0)

这是功能。

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)

}