焦点行动的时间延迟

时间:2015-10-20 02:13:26

标签: ios swift focus

我有一个收藏视图,并且安排它的方式是在屏幕的一侧有一个列,并根据收集项目的焦点替换内容。

如果关注某个项目的时间超过0.5秒,我希望能够更换内容。

这是我目前拥有的,它会立即交换数据。

if self.focused {
   self.label.alpha = 1
   self.priceLabel.alpha = 1
   if self.representedDataItem?.imageUrl! == "https://s3-us-west-2.amazonaws.com/random/image.png" ||
      self.representedDataItem?.imageUrl! == "" {
           self.backgroundImage.image = UIImage(named: "titleImage")
   }
   else {
      ImageCache.sharedLoader.imageForUrl((self.representedDataItem?.imageUrl!)!, completionHandler:{(image: UIImage?, url: String) in
           self.backgroundImage.image = image!
      })
   }
}
else {
   self.label.alpha = 0.2
   self.priceLabel.alpha = 0.2
}

2 个答案:

答案 0 :(得分:0)

不知道self.focused如何确定,假设您已经处理过它。因此,您可以使用GCD来延迟重点执行。

dispatch_after(dispatch_time(DISPATCH_TIME_NOW,  Int64(0.5 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
    // code to be executed after 0.5 sec
}

答案 1 :(得分:0)

if self.focused {
   self.label.alpha = 1
   self.priceLabel.alpha = 1
}
else {
   self.label.alpha = 0.2
   self.priceLabel.alpha = 0.2
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,  Int64(0.5 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
    // code to be executed after 0.5 sec
    if self.focused {
        if self.representedDataItem?.imageUrl! == "https://s3-us-west-2.amazonaws.com/random/image.png" ||
           self.representedDataItem?.imageUrl! == "" {
               self.backgroundImage.image = UIImage(named: "titleImage")
        }
        else {
            ImageCache.sharedLoader.imageForUrl((self.representedDataItem?.imageUrl!)!, completionHandler:{(image: UIImage?, url: String) in
                self.backgroundImage.image = image!
            })
        }
    }
}