【问题标题】:UIImageView animation in Swift with time delay带有时间延迟的 Swift 中的 UIImageView 动画
【发布时间】:2015-02-20 23:46:50
【问题描述】:

我遇到了这个问题,我希望 UIImage 在两秒钟内发光,然后恢复到正常状态。

鉴于这个问题,这是我目前遇到的问题:

我引用了从 0 到 9 的图像视图。

@IBOutlet weak var imageOne: UIImageView!
@IBOutlet weak var imageTwo: UIImageView!

等等

然后我将它们添加到 viewDiDLoad() 函数中的 SubViews 中:

override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(imageOne)
        self.view.addSubview(imageTwo)
        etc.
}

这是我对着色功能的实现:

func colorize(imageView: UIImageView, color: CGColorRef) {
        imageView.layer.shadowColor = color
        imageView.layer.shadowRadius = 7.0
        imageView.layer.shadowOpacity = 0.9
        imageView.layer.shadowOffset = CGSizeZero
        imageView.layer.masksToBounds = false
}

现在,我正在尝试为通过此调用的两个图像视图设置动画。 a 和 b 只是从前一个视图中获得的随机数。在本例中,它们将是 1 和 3。:

UIView.animateWithDuration(2.0, delay: 0, options: nil, animations: { () -> Void in
        self.colorize(labelArray[a.toInt()!], color:self.green)
}, completion: nil)

UIView.animateWithDuration(2.0, delay: 2.0, options: nil, animations: { () -> Void in
        self.colorize(labelArray[b.toInt()!], color:self.cyan)
}, completion: nil)

现在,这就是视图之前的样子 -

这就是之后的样子,但两个动画同时发生。没有过渡。它只是自动应用发光 -

感谢您的帮助!

【问题讨论】:

    标签: ios image animation swift uiimageview


    【解决方案1】:

    由于您要为UIImageView 层设置动画,请尝试使用Core Animation 而不是UIView 动画块,因为必须使用Core Animation 为视图的阴影层设置动画。如果您只是想淡入阴影,请尝试使用以下动画设置阴影不透明度:

    override func performGlowAnimations {
    
        self.colorize(labelArray[a.toInt()!], color:self.green)
    
        // Delay the second call by 2 seconds
        let delay = 2.0 * Double(NSEC_PER_SEC)
        var time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
        dispatch_after(time, dispatch_get_main_queue(), {
            self.colorize(labelArray[b.toInt()!], color:self.cyan)
        })
    }
    
    func colorize(imageView: UIImageView, color: CGColorRef) {
    
        // Set the image's shadowColor, radius, offset, and
        // set masks to bounds to false
        imageView.layer.shadowColor = color
        imageView.layer.shadowRadius = 7.0
        imageView.layer.shadowOffset = CGSizeZero
        imageView.layer.masksToBounds = false
    
        // Animate the shadow opacity to "fade it in"
        let shadowAnim = CABasicAnimation()
        shadowAnim.keyPath = "shadowOpacity"
        shadowAnim.fromValue = NSNumber(float: 0.0)
        shadowAnim.toValue = NSNumber(float: 0.9)
        shadowAnim.duration = 2.0
    
        imageView.layer.addAnimation(shadowAnim, forKey: "shadowOpacity")
        imageView.layer.shadowOpacity = 0.9
    }
    

    【讨论】:

      猜你喜欢
      • 2012-02-08
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 2013-08-18
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 2017-04-04
      相关资源
      最近更新 更多