图像不会动画

时间:2015-08-31 22:46:00

标签: ios xcode image swift animation

我一直试图通过使用基于图像的动画来制作一些骰子图像,并在显示随机图像之前在骰子上显示不同数字的几个图像。以下是ViewController的代码:

import UIKit
import Foundation

class ViewController: UIViewController {

    // Links to the image views
    @IBOutlet weak var dieImage0: UIImageView!
    @IBOutlet weak var dieImage1: UIImageView!
    @IBOutlet weak var dieImage2: UIImageView!
    @IBOutlet weak var dieImage3: UIImageView!
    @IBOutlet weak var dieImage4: UIImageView!
    @IBOutlet weak var dieImage5: UIImageView!
    @IBOutlet weak var dieImage6: UIImageView!
    @IBOutlet weak var dieImage7: UIImageView!

    // Setting the RandomImages function to a constant 
    let randomImages = RandomImages()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Array of animation images
        var animationImages:[UIImage] = [
            UIImage(named: "dicey-die1")!,
            UIImage(named: "dicey-die2")!,
            UIImage(named: "dicey-die3")!,
            UIImage(named: "dicey-die4")!,
            UIImage(named: "dicey-die5")!,
            UIImage(named: "dicey-die6")!,
            UIImage(named: "dicey-die1")!,
            UIImage(named: "dicey-die2")!,
            UIImage(named: "dicey-die3")!,
            UIImage(named: "dicey-die4")!,
            UIImage(named: "dicey-die5")!,
            UIImage(named: "dicey-die6")!,
            UIImage(named: "dicey-die1")!,
            UIImage(named: "dicey-die2")!,
            UIImage(named: "dicey-die3")!,
            UIImage(named: "dicey-die4")!,
            UIImage(named: "dicey-die5")!,
            UIImage(named: "dicey-die6")!,
            UIImage(named: "dicey-die1")!,
            UIImage(named: "dicey-die2")!,
            UIImage(named: "dicey-die3")!,
            UIImage(named: "dicey-die4")!,
            UIImage(named: "dicey-die5")!,
            UIImage(named: "dicey-die6")!
        ]

        // Animation duration and repeat count
        var animationDuration: NSTimeInterval = 1.0
        var animationRepeatCount: Int = 1


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // Motion to roll dice
    override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent) {
        println("Motion Began")

    }

    override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) {

        // Trying to get one of the dice to roll
        self.dieImage0.startAnimating() 

        dieImage0.image = randomImages.randomDice();
        dieImage1.image = randomImages.randomDice();
        dieImage2.image = randomImages.randomDice();
        dieImage3.image = randomImages.randomDice();
        dieImage4.image = randomImages.randomDice();
        dieImage5.image = randomImages.randomDice();
        dieImage6.image = randomImages.randomDice();
        dieImage7.image = randomImages.randomDice();

        println("Motion Ended")
    }

    override func motionCancelled(motion: UIEventSubtype, withEvent event: UIEvent) {
        println("Motion Cancelled")
    }


}

RandomImages文件的代码:

import Foundation
import UIKit

struct RandomImages {

    var diceImages:[UIImage] = [
        UIImage(named: "dicey-die1")!,
        UIImage(named: "dicey-die2")!,
        UIImage(named: "dicey-die3")!,
        UIImage(named: "dicey-die4")!,
        UIImage(named: "dicey-die5")!,
        UIImage(named: "dicey-die6")!
    ]

    func randomDice() -> UIImage {
        var unsignedArrayCount = UInt32(diceImages.count)
        var unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
        var randomNumber = Int(unsignedRandomNumber)

        return diceImages[randomNumber]
    }

}

Storyboard and assistant editor

还需要有关如何遵循DRY校长的任何帮助。

1 个答案:

答案 0 :(得分:1)

你的代码没有多大意义。您正在视图控制器上创建名为animationImagesanimationDurationanimationRepeatCount的变量。这没有任何作用。

您需要在UIImageView上设置这些属性。假设dieImage0UIImageView,它被视为视图控制器中的插座,代码可能如下所示:

func animateDieImage0
{
  dieImage0.animationImages = [
            UIImage(named: "dicey-die1")!,
            UIImage(named: "dicey-die2")!,
            UIImage(named: "dicey-die3")!,
            UIImage(named: "dicey-die4")!,
            UIImage(named: "dicey-die5")!,
            UIImage(named: "dicey-die6")!,
            UIImage(named: "dicey-die1")!,
            UIImage(named: "dicey-die2")!,
            UIImage(named: "dicey-die3")!,
            UIImage(named: "dicey-die4")!,
            UIImage(named: "dicey-die5")!,
            UIImage(named: "dicey-die6")!,
            UIImage(named: "dicey-die1")!,
            UIImage(named: "dicey-die2")!,
            UIImage(named: "dicey-die3")!,
            UIImage(named: "dicey-die4")!,
            UIImage(named: "dicey-die5")!,
            UIImage(named: "dicey-die6")!,
            UIImage(named: "dicey-die1")!,
            UIImage(named: "dicey-die2")!,
            UIImage(named: "dicey-die3")!,
            UIImage(named: "dicey-die4")!,
            UIImage(named: "dicey-die5")!,
            UIImage(named: "dicey-die6")!
        ]
  dieImage0.animationDuration = 1.0
  dieImage0.animationRepeatCount = 1
  dieImage0.startAnimating()
}

顺便说一句,多次重复相同的6张图像是制作动画重复的错误方法。 (这是对内存的巨大浪费。)这就是repeatCount的用途。