2015-08-31 3 views
0

Я пытаюсь получить некоторые изображения кубиков для анимации с помощью анимации на основе изображения и отображения нескольких изображений разных чисел на кости, прежде чем они отобразят случайное изображение. Вот код для 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

Любая помощь на том, как следовать СУХОЙ принципала также хотел.

ответ

1

Ваш код не имеет большого смысла. Вы создаете переменные на вашем контроллере вида с именем animationImages, animationDuration и animationRepeatCount. Это ничего не делает.

Вам необходимо установить эти свойства на UIImageView. Предполагая, что dieImage0 является UIImageView, который подключен как выход в контроллере представления, код может выглядеть следующим образом:

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.

+0

Я помещаю этот код в свое «motionEnded» или «viewDidLoad»? Я также получаю сообщение об ошибке, которое говорит о том, чтобы положить точку с запятой между «animationImages» и «:». –

+0

Я не удалял тип из объявления переменной. Я отредактировал свой ответ. Что касается того, где вы помещаете этот код, это зависит. Когда вы хотите, чтобы ваше изображение начинало анимацию? Когда сначала отображается контроллер просмотра или в какой-то другой точке? Этот код запустит анимацию. Если вы хотите, чтобы изображение сразу отображалось, поместите его в viewDidLoad. Если вы хотите, чтобы он запускался в вашем событии motionEnded, поместите его туда. –

+0

Возможно, вы должны настроить массив animationImages в viewDidLoad, а затем вызвать startAnimating(), где бы вы ни хотели запускать свою анимацию. –

Смежные вопросы