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]
}
}
如何遵循DRY主任何帮助,也想。
我把这段代码放在我的'motionEnded'或'viewDidLoad'中吗?我还得到一个错误,说在'animationImages'和':'之间加上分号。 –
我没有从你的变量声明中删除这个类型。我编辑了我的答案。至于你把代码放在哪里,这取决于。你什么时候想让你的图像视图开始动画?当视图控制器第一次显示时,或在其他点?该代码将导致动画开始。如果您希望立即将图像视图设置为动画,请将其放入viewDidLoad中。如果你想让它触发你的motionEnded事件,把它放在那里。 –
您应该在viewDidLoad中设置animationImages数组,然后在需要触发动画的任何位置调用startAnimating()。 –