Reputation: 93
Okay so the purpose of the following code is to create an UI image animation with an initial static image, which upon being activated through a UIbutton action it will shuffle through a series of images from the assets.xcassts and then after 2 cycles stop. Also, when the animation stops the static UI image will have changed to a random image from assets.xcasst. The code works as desired at first, however each time you trigger the UIbutton and the images shuffle and the static UI image is changed, the repeatcount of the animation increases by 1 loop.
var myImages = [UIImage]()
@IBAction func playRoundTapped(sender: UIButton) {
for i in 1...3
{
myImages.append(UIImage(named: "\(i)")!)
}
firstDiceImageView.animationImages = myImages
firstDiceImageView.animationRepeatCount = 2
secondDiceImageView.animationImages = myImages
secondDiceImageView.animationRepeatCount = 2
thirdDiceImageView.animationImages = myImages
thirdDiceImageView.animationRepeatCount = 2
forthDiceImageView.animationImages = myImages
forthDiceImageView.animationRepeatCount = 2
firstDiceImageView.startAnimating()
secondDiceImageView.startAnimating()
thirdDiceImageView.startAnimating()
forthDiceImageView.startAnimating()
}
Above is the code for the animation. The animationRepeatCount is set to 2 cycles for each animation. On the first button click it cycles twice as wanted, however on the second button tap it cycles 3 times and on the third it cycles 4 times.
Below is the complete ViewController.swift file with all the code.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var begButton: UISwitch!
@IBOutlet weak var intButton: UISwitch!
@IBOutlet weak var advButton: UISwitch!
@IBOutlet weak var playRoundButton: UIButton!
@IBOutlet weak var forthDiceImageView: UIImageView!
@IBOutlet weak var thirdDiceImageView: UIImageView!
@IBOutlet weak var secondDiceImageView: UIImageView!
@IBOutlet weak var firstDiceImageView: UIImageView!
var myImages = [UIImage]()
@IBAction func playRoundTapped(sender: UIButton) {
for i in 1...3
{
myImages.append(UIImage(named: "\(i)")!)
}
firstDiceImageView.animationImages = myImages
firstDiceImageView.animationRepeatCount = 2
secondDiceImageView.animationImages = myImages
secondDiceImageView.animationRepeatCount = 2
thirdDiceImageView.animationImages = myImages
thirdDiceImageView.animationRepeatCount = 2
forthDiceImageView.animationImages = myImages
forthDiceImageView.animationRepeatCount = 2
firstDiceImageView.startAnimating()
secondDiceImageView.startAnimating()
thirdDiceImageView.startAnimating()
forthDiceImageView.startAnimating()
}
@IBAction func playRoundTapped2(sender: AnyObject) {
let firstRandomNumber = arc4random_uniform(2) + 1
let firstCardString:String = String(format: "side%i",
firstRandomNumber)
let secondRandomNumber = arc4random_uniform(1) + 1
let secondCardString:String = String(format: "invert%i",
secondRandomNumber)
self.firstDiceImageView.image = UIImage(named: firstCardString)
self.secondDiceImageView.image = UIImage(named: secondCardString)
self.thirdDiceImageView.image = UIImage(named: "card_default")
self.forthDiceImageView.image = UIImage(named: "card_default")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.firstDiceImageView.image = UIImage(named: "card_default")
self.secondDiceImageView.image = UIImage(named: "card_default")
self.thirdDiceImageView.image = UIImage(named: "card_default")
self.forthDiceImageView.image = UIImage(named: "card_default")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Upvotes: 0
Views: 153
Reputation: 4044
have a look at this part of your code below...
var myImages = [UIImage]()
@IBAction func playRoundTapped(sender: UIButton) {
for i in 1...3
{
myImages.append(UIImage(named: "\(i)")!)
}
firstDiceImageView.animationImages = myImages
firstDiceImageView.animationRepeatCount = 2
secondDiceImageView.animationImages = myImages
secondDiceImageView.animationRepeatCount = 2
thirdDiceImageView.animationImages = myImages
thirdDiceImageView.animationRepeatCount = 2
forthDiceImageView.animationImages = myImages
forthDiceImageView.animationRepeatCount = 2
firstDiceImageView.startAnimating()
secondDiceImageView.startAnimating()
thirdDiceImageView.startAnimating()
forthDiceImageView.startAnimating()
}
you are creating the myImages
array
before you call the function. Then when you call the function, you append the images to it. the next time you call the function you AGAIN, append 3 more images to it. You never empty your array. At the beggining of the @IBAction you can use myImages.removeAll
to empty the array just before you append the 3 images.
Upvotes: 1