我要添加动画的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
。 @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
)根本不执行任何操作!更新2:
这是我用于动画的代码:
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
}
现在一切正常!
最佳答案
要创建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
上遇到了问题,因为您在分配动画随机图像之前就开始播放动画。首先分配图像,然后开始动画处理。关于ios - 使用循环将属性添加到多个UIImageViews,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32358297/