Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用swift设置不透明度动画?_Swift_Uiimageview_Core Animation_Cabasicanimation_Nsvalue - Fatal编程技术网

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

如何使用swift设置不透明度动画?,swift,uiimageview,core-animation,cabasicanimation,nsvalue,Swift,Uiimageview,Core Animation,Cabasicanimation,Nsvalue,有人能给我提供一个在swift中设置图像视图不透明度动画的例子吗 在目标c中我甚至找不到一个好的例子 func showCorrectImage(element: AnyObject){ var animation : CABasicAnimation = CABasicAnimation(keyPath: "opacity"); animation.delegate = self animation.fromValue = NSValue(nonretainedO

有人能给我提供一个在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)
}
我想我大部分都是对的(虽然不完全确定),有人能帮我吗

元素=图像视图


提前谢谢~

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

imageView.alpha = 0
UIView.animateWithDuration(1.0) {
        imageView.alpha = 1
}

如果有人希望像OP最初尝试的那样使用Cabasicanization,那么他的原始代码的一个问题是他使用NSValue作为toValue。我把它换成了NSNumber,它对我起了作用。像这样

    fadeToVisible.fromValue = NSNumber(float: 0.0)

你也可以这样写:

animation.fromValue = 0.0
animation.toValue = 1.0
let animation = CABasicAnimation(#keyPath(CALayer.opacity))
animation.fromValue = 0.0
animation.toValue = 1.0
animation.duration = 1.0
animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
element.layer?.addAnimation(animation, forKey: "fade")
整个代码应该是这样的:

animation.fromValue = 0.0
animation.toValue = 1.0
let animation = CABasicAnimation(#keyPath(CALayer.opacity))
animation.fromValue = 0.0
animation.toValue = 1.0
animation.duration = 1.0
animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
element.layer?.addAnimation(animation, forKey: "fade")

您可以使用此扩展方法(除非您要修改其本身):

extensioncalayer{
func闪烁(持续时间:时间间隔)->可观察{
let flash=cabasicaniation(关键路径:“不透明度”)
flash.fromValue=NSNumber(值:0)
flash.toValue=NSNumber(值:1)
flash.duration=持续时间
flash.autoreverses=true
removeAnimation(forKey:“flashAnimation”)
添加(flash,forKey:“flashAnimation”)
不透明度=0//将图层中的实际数据值更改为最终值
}
}

@PugLvr28看起来你是新来的。如果Swipesight的答案解决了你的问题,你应该接受它。