Ios SWIFT中imageview上的随机/不规则脉冲动画

Ios SWIFT中imageview上的随机/不规则脉冲动画,ios,swift,animation,uiviewanimation,Ios,Swift,Animation,Uiviewanimation,我需要在imageview上实现一个随机的“脉冲”动画。 随机的,因为我不想要一个简单的循环动画,它每x秒重复一次。脉动必须是不规则的 我需要一个随机延迟,随机持续时间,如果可能的话,fromValue和toValue的随机值 我的简单脉冲动画代码,每5秒重复一次,不透明度从1到0: var pulseAnimation:CABasicAnimation = CABasicAnimation(keyPath: "opacity") pulseAnimation.duration

我需要在imageview上实现一个随机的“脉冲”动画。 随机的,因为我不想要一个简单的循环动画,它每x秒重复一次。脉动必须是不规则的

我需要一个随机延迟,随机持续时间,如果可能的话,fromValue和toValue的随机值

我的简单脉冲动画代码,每5秒重复一次,不透明度从1到0:

    var pulseAnimation:CABasicAnimation = CABasicAnimation(keyPath: "opacity")
    pulseAnimation.duration = 5.0
    pulseAnimation.fromValue = NSNumber(float: 0.0)
    pulseAnimation.toValue = NSNumber(float: 1.0)
    pulseAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    pulseAnimation.autoreverses = true
    pulseAnimation.repeatCount = Float.infinity
    myimageview.layer.addAnimation(pulseAnimation, forKey: "opacity")
如果我写下这样的话:

    pulseAnimation.duration = arc4random(....) //I will have a "fixed" random value
如何实现不规则值?

arc4random()
arc4random\u uniform()
都是随机的。不需要种子设定,并且它们具有加密质量

对于范围,请使用
arc4random\u uniform()
,而不是在
arc4random()
上使用模块运算符

您不想使用的是
rand()

arc4random()
arc4random\u uniform()
都是随机的。不需要种子设定,并且它们具有加密质量

对于范围,请使用
arc4random\u uniform()
,而不是在
arc4random()
上使用模块运算符

您不想使用的是
rand()

arc4random()
arc4random\u uniform()
都是随机的。不需要种子设定,并且它们具有加密质量

对于范围,请使用
arc4random\u uniform()
,而不是在
arc4random()
上使用模块运算符

您不想使用的是
rand()

arc4random()
arc4random\u uniform()
都是随机的。不需要种子设定,并且它们具有加密质量

对于范围,请使用
arc4random\u uniform()
,而不是在
arc4random()
上使用模块运算符

您不想使用的是
rand()

好的,我找到了一个方法:

    for loopNumber in 1...5 {

        let myImageView = UIImageView()
        myImageView.image = UIImage(named: "nameImageView")
        myImageView.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
        self.view.addSubview(myImageView)

        var pulseAnimation = CABasicAnimation(keyPath: "opacity")

        pulseAnimation.fromValue = Double(arc4random_uniform(6)+1)/10
        pulseAnimation.toValue = Double(arc4random_uniform(6)+2)/10
        pulseAnimation.duration = Double(loopNumber)            
        pulseAnimation.repeatCount = Float.infinity

        myImageView.layer.addAnimation(pulseAnimation, forKey: "opacity")
    }
好的,我找到了一个方法:

    for loopNumber in 1...5 {

        let myImageView = UIImageView()
        myImageView.image = UIImage(named: "nameImageView")
        myImageView.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
        self.view.addSubview(myImageView)

        var pulseAnimation = CABasicAnimation(keyPath: "opacity")

        pulseAnimation.fromValue = Double(arc4random_uniform(6)+1)/10
        pulseAnimation.toValue = Double(arc4random_uniform(6)+2)/10
        pulseAnimation.duration = Double(loopNumber)            
        pulseAnimation.repeatCount = Float.infinity

        myImageView.layer.addAnimation(pulseAnimation, forKey: "opacity")
    }
好的,我找到了一个方法:

    for loopNumber in 1...5 {

        let myImageView = UIImageView()
        myImageView.image = UIImage(named: "nameImageView")
        myImageView.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
        self.view.addSubview(myImageView)

        var pulseAnimation = CABasicAnimation(keyPath: "opacity")

        pulseAnimation.fromValue = Double(arc4random_uniform(6)+1)/10
        pulseAnimation.toValue = Double(arc4random_uniform(6)+2)/10
        pulseAnimation.duration = Double(loopNumber)            
        pulseAnimation.repeatCount = Float.infinity

        myImageView.layer.addAnimation(pulseAnimation, forKey: "opacity")
    }
好的,我找到了一个方法:

    for loopNumber in 1...5 {

        let myImageView = UIImageView()
        myImageView.image = UIImage(named: "nameImageView")
        myImageView.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
        self.view.addSubview(myImageView)

        var pulseAnimation = CABasicAnimation(keyPath: "opacity")

        pulseAnimation.fromValue = Double(arc4random_uniform(6)+1)/10
        pulseAnimation.toValue = Double(arc4random_uniform(6)+2)/10
        pulseAnimation.duration = Double(loopNumber)            
        pulseAnimation.repeatCount = Float.infinity

        myImageView.layer.addAnimation(pulseAnimation, forKey: "opacity")
    }