如何使用swift动画不透明度?

Pug*_*r28 6 core-animation uiimageview cabasicanimation nsvalue swift

有人可以请给我一个动画图像视图的不透明度的示例吗?

我甚至无法在目标c中找到一个好的例子

func showCorrectImage(element: AnyObject){

    var animation : CABasicAnimation = CABasicAnimation(keyPath: "opacity");

    animation.delegate = self

    animation.fromValue = NSValue(nonretainedObject: 0.0)
    animation.toValue = NSValue(nonretainedObject: 1.0)

    animation.duration = 1.0

    element.layer?.addAnimation(animation, forKey: nil)
}
Run Code Online (Sandbox Code Playgroud)

我认为我有大部分权利(尽管不完全确定),有人可以帮助我吗?

element =图像视图

提前谢谢!〜

yli*_*x81 13

如果你需要一个简单的动画,为什么不使用UIView的animateWithDuration:animations:方法呢?

imageView.alpha = 0
UIView.animateWithDuration(1.0) {
        imageView.alpha = 1
}
Run Code Online (Sandbox Code Playgroud)


小智 6

您也可以这样写:

animation.fromValue = 0.0
animation.toValue = 1.0
Run Code Online (Sandbox Code Playgroud)

整个代码应如下所示:

let animation = CABasicAnimation(keyPath: "opacity")
animation.delegate = self
animation.fromValue = 0.0
animation.toValue = 1.0
animation.duration = 1.0
animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
element.layer?.addAnimation(animation, forKey: "fade")
Run Code Online (Sandbox Code Playgroud)