Using a loop for adding attributes to multiple UII

2020-05-06 17:01发布

问题:

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?

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


    }

And for starting the animation:

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")
    }

I want to animate each dieImage

Edit

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.

Update:

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!

Update 2:

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
}

Everything works now!

回答1:

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)")! }
}

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)")! }

You want to do that 4 times, and add all the arrays together. You do that with the call to (0..<4).reduce([UIImage]()) { ... }

The result will be a single array with 24 images.

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()
}

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()
    }

}

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.



回答2:

Yup, put the images in an array and loop through the array.

for var i = 0; i < dieImages.count; i++ {
    dieImages[i].startAnimating()
}

Does that help? A little confused about your intentions, but I think this answers your question