最初,当我只是为我拥有的一个 UIImageView 设置动画时,我让这段代码工作。但后来我将它更改为动画几个动态创建的 UIImageViews,但是由于它们是在 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编辑