我有8 UImageView
个我正在添加动画。我知道我可以制作动画八次,但是我可以使用一个循环 - 也许是插值 - 吗?
这是我的动画代码:
override func viewDidLoad() {
super.viewDidLoad()
self.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")!
]
self.dieImage0.animationRepeatCount = 1
self.dieImage0.animationDuration = 1.0
}
开始动画:
override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) {
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")
}
我想为每个dieImage
我有几个UIImageView
个@IBOutlet
s我想制作动画。
@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!
如何循环浏览它们而不是为每个动画制作单独的动画,我已经有一个动画设置;见上文。
除了两个骰子反叛之外,一切都按照惯例运作。第一个(dieImage0
)始终位于1,第二个(dieImage5
)根本不会做任何事情!
这是我用于动画的代码:
let dieImages = [dieImage0, dieImage1, dieImage2, dieImage3, dieImage4, dieImage5, dieImage6, dieImage7]
for die in dieImages {
die.animationImages = [
UIImage(named: "dicey-die2")!,
UIImage(named: "dicey-die6")!,
UIImage(named: "dicey-die1")!,
UIImage(named: "dicey-die4")!,
UIImage(named: "dicey-die3")!,
UIImage(named: "dicey-die5")!,
UIImage(named: "dicey-die3")!,
UIImage(named: "dicey-die1")!,
UIImage(named: "dicey-die6")!,
UIImage(named: "dicey-die3")!,
UIImage(named: "dicey-die5")!,
UIImage(named: "dicey-die2")!,
UIImage(named: "dicey-die4")!
]
die.animationRepeatCount = 1
die.animationDuration = 1.0
}
现在一切正常!
答案 0 :(得分:1)
要创建animationImages
,我会这样做:
dieImage0.animationImages = (0..<4).reduce([UIImage]()) { images, _ in
return images + (1..<7).map { UIImage(named: "dicey-die\($0)")! }
}
看起来animationImages
包含24 UIImages
- 4组6张图片,其中图片的名称为"dicey-dieN"
,(N
被数字替换在(1..<7)
范围内。)
您可以创建一个包含六个图像的单个数组:
let images = (1..<7).map { UIImage(named: "dicey-die\($0)")! }
您希望这样做4次,并将所有阵列添加到一起。您可以通过调用(0..<4).reduce([UIImage]()) { ... }
结果将是包含24张图像的单个数组。
然后,正如@Chris Slowik建议的那样,创建一个dieImages
数组,然后遍历它们以分配随机图像:
let dieImages = [dieImage0, dieImage1, dieImage2, dieImage3, dieImage4, dieImage5, dieImage6, dieImage7]
for dieImage in dieImages {
dieImage.image = randomImages.randomDice()
dieImage.startAnimating()
}
您的整个motionEnded
方法应该看起来像这样:
override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) {
let dieImages = [dieImage0, dieImage1, dieImage2, dieImage3, dieImage4, dieImage5, dieImage6, dieImage7]
for dieImage in dieImages {
dieImage.image = randomImages.randomDice()
dieImage.startAnimating()
}
}
我认为您遇到dieImage0
的问题,因为您在为其分配随机图像之前开始动画。首先分配图像,然后开始设置动画。
答案 1 :(得分:0)
是的,将图像放在一个数组中并循环遍历数组。
for var i = 0; i < dieImages.count; i++ {
dieImages[i].startAnimating()
}
这有帮助吗?对你的意图有点困惑,但我认为这回答了你的问题