0

Initially I had this code working when I was just animating the one UIImageView that I had. But then I changed it to animate several dynamically created UIImageViews, however since they are dynamically created inside a for loop, I'm finding it difficult to animate them as I did the initial one.

override func viewDidLoad() {
    super.viewDidLoad()

    var sprite: UIImage = UIImage(named: "sprites/areaLocatorSprite.png")!

    var locations:NSArray = animal[eventData]["locations"] as NSArray

    for var i = 0; i < locations.count; i++ {

        println(locations[i]["locationx"])
        var locationx = locations[i]["locationx"] as String
        var locationy = locations[i]["locationy"] as String

        let x = NSNumberFormatter().numberFromString(locationx)
        let y = NSNumberFormatter().numberFromString(locationy)
        let cgfloatx = CGFloat(x!)
        let cgfloaty = CGFloat(y!)

        var mapSprite: UIImageView
        mapSprite = UIImageView(image: sprite)

        mapSprite.frame = CGRectMake(cgfloatx,cgfloaty,10,10)
        townMap.addSubview(mapSprite)

        timer = NSTimer.scheduledTimerWithTimeInterval(0.35, target: self, selector: Selector("flash"), userInfo: nil, repeats: true)

    }

}

func flash() {
    var mapSprite:UIImageView?

    if mapSprite?.alpha == 1 {
        mapSprite?.alpha = 0
    } else {
        mapSprite?.alpha = 1
    }
}

This does not work as the mapSprite in the flash function is different to the one in the for loop. How can I refer to the one in the for loop and then animate it? Or would there be a better alternative to what I'm currently doing?

Many thanks!

EDIT Using Xcode 6.2

Javz
  • 318
  • 1
  • 3
  • 13

1 Answers1

0

You need to store the views into a property and then enumerate that property each time your timer event is fired

var sprites: [UIImageView]?

override func viewDidLoad() {
  super.viewDidLoad()

  var sprite = UIImage(named: "sprites/areaLocatorSprite.png")!

  var locations:NSArray = animal[eventData]["locations"] as NSArray

  self.sprites = map(locations) {
    var locationx = $0["locationx"] as String
    var locationy = $0["locationy"] as String

    let x = NSNumberFormatter().numberFromString(locationx)
    let y = NSNumberFormatter().numberFromString(locationy)
    let cgfloatx = CGFloat(x!)
    let cgfloaty = CGFloat(y!)

    var mapSprite = UIImageView(image: sprite)

    mapSprite.frame = CGRectMake(cgfloatx,cgfloaty,10,10)
    townMap.addSubview(mapSprite)

    return mapSprite
  }

  timer = NSTimer.scheduledTimerWithTimeInterval(0.35, target: self, selector: Selector("flash"), userInfo: nil, repeats: true)
}

func flash() {
  if let sprites = self.sprites {
    for sprite in sprites {
      sprite.alpha = sprite.alpha == 0 ? 1 : 0
    }
  }
}
Paul.s
  • 38,494
  • 5
  • 70
  • 88
  • I think I also should have mentioned I'm using the Xcode 6.2 as guard is something new to Swift2? As I had to Google what guard was and then had another issue with the layout for the for statement, again assuming something new with Swift2? Also the locations variable is an NSArray and does not have a map member; unless in Swift2 it does? Thanks, I really appreciate the help! – Javz Sep 22 '15 at 23:49
  • Updated again for Swift 1 – Paul.s Sep 22 '15 at 23:53
  • Thanks for the update but I've got two errors this time, first being '[UIImageView]? does not have a member named Generator', but solved after replacing ? with ! Second being 'braced block of statements is an unused closure' and 'expected { after if condition swift' unless I do this, but then it doesn't work func flash() { if let sprites = self.sprites{} else { for sprite in sprites { sprite.alpha = sprite.alpha == 0 ? 1 : 0 } } } I've been trying to fix it on my own, but seems like I've failed. Sorry and thanks again!! – Javz Sep 23 '15 at 00:11
  • I've fixed it by doing the following - func flash() { if let sprites = self.sprites{ for sprite in sprites { sprite.alpha = sprite.alpha == 0 ? 1 : 0 } } else { for sprite in sprites { sprite.alpha = sprite.alpha == 0 ? 1 : 0 } } } Please update your code to this, or a better version and I shall mark it as the correct answer :) Thanks! – Javz Sep 23 '15 at 00:34
  • Sorry there was a left over `else` in that line from when I converted from Swift 2 to Swift 1 (dangers of editing on a phone) – Paul.s Sep 23 '15 at 07:31
  • Still impressive! Thanks :) – Javz Sep 23 '15 at 13:44