objective-c – Swift CAKeyframe动画不显示图像
发布时间:2020-12-16 09:56:03 所属栏目:百科 来源:网络整理
导读:我使用iOS 8.1设置了一个非常简单的单视图应用程序(在 Swift中).我在主视图控制器视图中添加了一个UIImageView.我正在尝试使用CAKeyframeAnimation来动画一系列图像.我最初使用UIImageView animationImages属性工作正常,但我需要能够准确知道动画何时完成,因
我使用iOS 8.1设置了一个非常简单的单视图应用程序(在
Swift中).我在主视图控制器视图中添加了一个UIImageView.我正在尝试使用CAKeyframeAnimation来动画一系列图像.我最初使用UIImageView animationImages属性工作正常,但我需要能够准确知道动画何时完成,因此转移到CAKeyframeAnimation.
我的代码如下: class ViewController: UIViewController { @IBOutlet weak var imageView: UIImageView! var animation : CAKeyframeAnimation! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view,typically from a nib. let animationImages:[AnyObject] = [UIImage(named: "image-1")!,UIImage(named: "image-2")!,UIImage(named: "image-3")!,UIImage(named: "image-4")!] animation = CAKeyframeAnimation(keyPath: "contents") animation.calculationMode = kCAAnimationDiscrete animation.duration = 25 animation.values = animationImages animation.repeatCount = 25 animation.removedOnCompletion = false animation.fillMode = kCAFillModeForwards self.imageView.layer.addAnimation(animation,forKey: "contents") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 问题是动画不显示任何图像,我只是收到一个空白屏幕.上面的代码中是否有我遗漏的东西?如何显示动画? 解决方法
这条线永远不会起作用:
animation.values = animationImages 将其更改为: animation.values = animationImages.map {$0.CGImage as AnyObject} 原因是您正在尝试为此图层的“内容”键设置动画.但那是内容属性.但是contents属性必须设置为CGImage,而不是UIImage.相比之下,您的animationImages包含UIImages,而不是CGImages. 因此,您需要将UIImage数组转换为CGImage数组.此外,您正在尝试将此数组传递给Objective-C,其中NSArray必须仅包含对象;由于CGImage不是Objective-C中的一个对象,因此需要将它们作为AnyObject进行转换.这就是我的地图调用. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |