swift2 快速无限淡入淡出循环

cx6n0qe3  于 2022-11-06  发布在  Swift
关注(0)|答案(7)|浏览(196)

我怎样才能在swift中产生类似这样的效果:

我希望动画永远循环。

0vvn1miw

0vvn1miw1#

适用于iOS

UIViewAnimationOptions集合提供了不同的方便选项来实现漂亮和复杂动画的组合。对于您的特定场景,您将需要其中的两个选项。

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方法,请注意,我在options参数中提供了一个数组[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

f0brbegy

f0brbegy2#

适用于iOS

viewSquare为问题中蓝色正方形的名称。

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

vsnjm48y3#

雨燕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)
}
igetnqfo

igetnqfo4#

我想你是为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)
    }
}
zaq34kh6

zaq34kh65#

"雨燕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)

}
6jygbczu

6jygbczu6#

如果你想要可重复的淡入淡出动画,你可以使用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: AnimationKeyPath.opacity.rawValue)
       flash.duration = duration
        flash.fromValue = 1 // alpha
        flash.toValue = 0 // alpha
        flash.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
        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)
2vuwiymt

2vuwiymt7#

雨燕5

  • 2秒后渐隐
  • 暂停2秒,
  • 淡出2秒重复
func startAnimation() {
     UIView.animateKeyframes(withDuration: 6.0,
                                    delay: 0,
                                    options: [.repeat, .autoreverse, .calculationModeLinear]) {
        UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.165) { [weak self] in
            self?.view1.alpha = 0.0
        }
        UIView.addKeyframe(withRelativeStartTime: 0.165, relativeDuration: 0.165) { [weak self] in
            self?.view2.alpha = 1.0
        }
        UIView.addKeyframe(withRelativeStartTime: 0.66, relativeDuration: 0.165) { [weak self] in
            self?.view2.alpha = 0.0
        }
        UIView.addKeyframe(withRelativeStartTime: 0.825, relativeDuration: 0.165) { [weak self] in
            self?.view1.alpha = 1.0
        }
    }
}

相关问题