使用循环添加属性多的UIImageViews [英] Using a loop for adding attributes to multiple UIImageViews
问题描述
我有8 UImageView
S中的我添加动画。我知道我可以使动画八次,但我可以用一个循环-and也许interpolation-呢?
I have 8 UImageView
s that I am adding animations to. I know I can make the animation eight times, but can I use a loop -and maybe interpolation- for it?
下面是我的code为动画:
Here is my code for the animation:
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
s的 @IBOutlet
S中的我想动画。
I have several UIImageView
s with @IBOutlet
s that I want to animate.
@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!
我如何循环通过他们而不是使每个单独的动画,我已经有一个动画设置;见上面。
How do I loop through them instead of making a separate animation for each, I already have one animation setup; see above.
一切工作通缉除了两个骰子背叛。第一个( dieImage0
),始终如一的土地上1,第二个( dieImage5
)将不会在做什么所有!
Everything is working as wanted except two dice have rebelled. The first one (dieImage0
) consistently lands on a 1, the second one (dieImage5
) won't do anything at all!
这是code我用我的动画:
This is the code I used for my animation:
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
,我会做这样的:
To create the animationImages
, I would do this:
dieImage0.animationImages = (0..<4).reduce([UIImage]()) { images, _ in
return images + (1..<7).map { UIImage(named: "dicey-die\($0)")! }
}
看起来 animationImages
由24 UIImages
- 4套6幅图像,图像的地方名称为冒险二烯
( N
是由许多范围内取代( 1 ..&LT; 7)
)
It looks like animationImages
consists of 24 UIImages
- 4 sets of 6 images, where the name of the image is "dicey-dieN"
, (N
is replaced by a number in the range (1..<7)
.)
您可以创建六幅图像的这样一个数组:
You can create a single array of six images like this:
let images = (1..<7).map { UIImage(named: "dicey-die\($0)")! }
您想这样做4次,并添加所有的阵列在一起。你这样做与调用(0 .. 4;)。降低([UIImage的()){...}
You want to do that 4 times, and add all the arrays together. You do that with the call to (0..<4).reduce([UIImage]()) { ... }
其结果将是一个单个阵列24的图像。
The result will be a single array with 24 images.
然后,@克里斯斯沃维克建议,创建 dieImages
的数组,然后通过他们循环分配随机图像:
Then, as @Chris Slowik suggested, create an array of dieImages
and then loop through them to assign the random image:
let dieImages = [dieImage0, dieImage1, dieImage2, dieImage3, dieImage4, dieImage5, dieImage6, dieImage7]
for dieImage in dieImages {
dieImage.image = randomImages.randomDice()
dieImage.startAnimating()
}
您整个 motionEnded
办法也许应该是这个样子:
Your entire motionEnded
method should probably look something like this:
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
,因为你已经开始了动画,你赋予它随机图像之前。首先指定图像,然后启动动画。
I think you were running into problems with dieImage0
because you were starting the animation before you assigned it a random image. Assign the image first, and then start animating.
这篇关于使用循环添加属性多的UIImageViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!