使用循环向多个 UIImageViews 添加属性

Using a loop for adding attributes to multiple UIImageViews(使用循环向多个 UIImageViews 添加属性)
本文介绍了使用循环向多个 UIImageViews 添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 8 个要添加动画的 UImageView.我知道我可以制作八次动画,但我可以使用循环——也许是插值——来实现它吗?

I have 8 UImageViews 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?

这是我的动画代码:

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 我想制作动画.

I have several UIImageViews with @IBOutlets 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!

这是我用于动画的代码:

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 个图像组成,其中图像的名称是 "dicey-dieN", (N 替换为 (1..<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).reduce([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.

然后,正如@Chris Slowik 建议的那样,创建一个 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 添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)