ios - Swift - 动态创建 UIImageView 的动画

标签 ios swift uiimageview uiimage ios-animations

最初,当我只是为我拥有的一个 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

最佳答案

您需要将 View 存储到属性中,然后在每次触发计时器事件时枚举该属性

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

关于ios - Swift - 动态创建 UIImageView 的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32727959/

相关文章:

ios - 如何更改部分 UIImage 的颜色?

ios - UITableViewController 闪烁行为

ios - 如何删除 UITableViewCells 之间的分隔符间隙

iphone - 向下滚动表格时如何放大 uiimageview?

ios - iOS 模拟器中存在头文件,但 iOS 设备上不存在...?

ios - 快速圆形按钮

ios - 在方法参数中指定对象的协议(protocol)

swift - UIWebView 不加载 HTTPS 页面 : Error Domain=NSURLErrorDomain Code=-999 "The operation couldn’ t be completed.(NSURLErrorDomain 错误 -999。)”

ios - 如何像 wallapop 应用程序一样在表格 View 的标题中有一个可伸缩的图像 slider

ios - UIImageView 中的 UIViewContentModeScaleAspectFit 无法正常工作