是否有可能使一个UIView
焦点?或者我应该UIButton
对所有可能的视图使用自定义?
我试图覆盖canBecomeFocused
但什么也没发生。
是否有可能使一个UIView
焦点?或者我应该UIButton
对所有可能的视图使用自定义?
我试图覆盖canBecomeFocused
但什么也没发生。
所以问题是我没有注意到我的细胞得到了焦点。要结束这一点,您需要实现
1) 覆盖 canBecomeFocused
2) 覆盖“didUpdateFocusInContext:withAnimationCoordinator:”方法,以便能够将单元格突出显示为焦点
斯威夫特 2.3:
override func canBecomeFocused() -> Bool {
return true
}
override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
if context.nextFocusedView == self {
coordinator.addCoordinatedAnimations({ () -> Void in
self.layer.backgroundColor = UIColor.blueColor().colorWithAlphaComponent(0.2).CGColor
}, completion: nil)
} else if context.previouslyFocusedView == self {
coordinator.addCoordinatedAnimations({ () -> Void in
self.layer.backgroundColor = UIColor.clearColor().CGColor
}, completion: nil)
}
}
斯威夫特 3:
override var canBecomeFocused: Bool {
return true
}
override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
if context.nextFocusedView == self {
coordinator.addCoordinatedAnimations({ () -> Void in
self.layer.backgroundColor = UIColor.blue.withAlphaComponent(0.2).cgColor
}, completion: nil)
} else if context.previouslyFocusedView == self {
coordinator.addCoordinatedAnimations({ () -> Void in
self.layer.backgroundColor = UIColor.clear.cgColor
}, completion: nil)
}
}
似乎情况发生了变化,现在使视图具有焦点的正确方法是:
override var canBecomeFocused : Bool {
return true
}
Pavel 代码的 Swift 3 版本:
override var canBecomeFocused: Bool {
return true
}
override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
if context.nextFocusedView == self {
coordinator.addCoordinatedAnimations({ () -> Void in
self.layer.backgroundColor = UIColor.blue.withAlphaComponent(0.2).cgColor
}, completion: nil)
} else if context.previouslyFocusedView == self {
coordinator.addCoordinatedAnimations({ () -> Void in
self.layer.backgroundColor = UIColor.clear.cgColor
}, completion: nil)
}
}