0

最初は、私が持っていた 1 つの UIImageView をアニメーション化していたときに、このコードが機能していました。しかし、動的に作成されたいくつかのUIImageViewをアニメーション化するように変更しましたが、forループ内で動的に作成されるため、最初のようにアニメーション化するのが難しいと感じています.

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

これは、flash 関数内の mapSprite が for ループ内のものと異なるため、機能しません。for ループ内のものを参照してからアニメーション化するにはどうすればよいですか? または、私が現在行っていることのより良い代替手段はありますか?

どうもありがとう!

Xcode 6.2を使用して編集

4

1 に答える 1

0

ビューをプロパティに保存し、タイマー イベントが発生するたびにそのプロパティを列挙する必要があります。

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
    }
  }
}
于 2015-09-22T23:13:49.210 に答える