UIButton的动画图片并没有显示出快速

时间:2017-03-20 23:30:24

标签: ios swift animation uiimageview uibutton

这让我疯了。我已经花了两天时间查看了有关如何动画UIImageview的所有文档。但是,当我运行应用和代码(在UIButton中调用)时, viewDidLoad上没有显示动画图像

最后一个if语句甚至打印出来"是的,它正在运行"。但是模拟器或设备上没有显示任何内容。

更新:已添加func setImage(_ image: UIImage?, for state: UIControlState):功能。现在第一个图像加载并显示,但仍然没有动画......

还试过给按钮一个单独的Outlet - 相同的结果...... 我现在真的卡住了。

        var Cowboy:[UIImage] = [UIImage(named: "Cowboy__000")!, UIImage(named: "Cowboy__001")!,UIImage(named: "Cowboy__002")!,UIImage(named: "Cowboy__003")!,UIImage(named: "Cowboy__004")!,UIImage(named: "Cowboy__005")!,UIImage(named: "Cowboy__006")!,UIImage(named: "Cowboy__007")!,UIImage(named: "Cowboy__008")!, UIImage(named: "Cowboy__009")!]

        var currentAnimationImages = [UIImage]

        for i in 0..<Cowboy.count {

            currentAnimationImages.append(Cowboy[i])

        }

        //buttons stored in outlet collection
        print("currentAnimationImages", currentAnimationImages) //the array contains the images!
        buttons[0].setImage(currentAnimationImages.first, for: .normal) //is shown
        //nothing happens...
        buttons[0].imageView!.animationImages = currentAnimationImages
        buttons[0].imageView!.animationDuration = 0.2
        buttons[0].imageView!.animationRepeatCount = 10
        buttons[0].imageView!.startAnimating()
            if buttons[0].imageView!.isAnimating == true {
            print("yes, running") //this is printed out!
            } else {
            print("no, not running")
            }

需要所有帮助并表示赞赏。

1 个答案:

答案 0 :(得分:1)

您必须先致电func setImage(_ image: UIImage?, for state: UIControlState):获取要在动画制作之前显示的图片。

例如:

buttons[0]. setImage(currentAnimationImages[0], forState: .Normal)
// Now you can start animating
buttons[0].imageView!.animationImages = currentAnimationImages
buttons[0].imageView!.animationDuration = 0.2
buttons[0].imageView!.animationRepeatCount = 10
buttons[0].imageView!.startAnimating()