gpt4 book ai didi

ios - 使用循环向多个 UIImageView 添加属性

转载 作者:行者123 更新时间:2023-11-29 01:41:13 24 4
gpt4 key购买 nike

我有 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

制作动画

编辑

我有几个带有 @IBOutletUIImageView 我想制作动画。

    @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 - 使用循环向多个 UIImageView 添加属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32358297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com