好吧,回答我的問題:)
所以顯得有些有些意見是「可聚焦」上tvOS出的即裝即用,和其他人必須被指示這樣做。
我終於結束了使用UITextView
,它具有selectable
屬性,但如果不是默認情況下這些可聚焦視圖之一。必須禁用TextView
的編輯才能使其看起來像UILabel
。此外,目前存在一個錯誤,它可以防止您使用Interface Builder中的selectable
屬性,但可以使用代碼。
自然地,canBecomeFocused()
和也必須實施。您還需要通過UIViewController
,因爲UITextView
無法呈現模態視圖控制器。貝婁是我最終創造的。
class FocusableText: UITextView {
var data: String?
var parentView: UIViewController?
override func awakeFromNib() {
super.awakeFromNib()
let tap = UITapGestureRecognizer(target: self, action: "tapped:")
tap.allowedPressTypes = [NSNumber(integer: UIPressType.Select.rawValue)]
self.addGestureRecognizer(tap)
}
func tapped(gesture: UITapGestureRecognizer) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let descriptionView = storyboard.instantiateViewControllerWithIdentifier("descriptionView") as? DescriptionViewController {
if let view = parentView {
if let show = show {
descriptionView.descriptionText = self.data
view.modalPresentationStyle = UIModalPresentationStyle.OverFullScreen
view.presentViewController(descriptionView, animated: true, completion: nil)
}
}
}
}
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.blackColor().colorWithAlphaComponent(0.2).CGColor
}, completion: nil)
} else if context.previouslyFocusedView == self {
coordinator.addCoordinatedAnimations({() -> Void in
self.layer.backgroundColor = UIColor.clearColor().CGColor
}, completion: nil)
}
}
}
感謝這個答案,我修改了一下我的項目。 https://gist.github.com/Cedrick84/8922a0af730c39c05794 – Cedrick
最近有什麼改變與tvOS會導致這不起作用?無論我嘗試什麼,我仍無法獲得TextView來獲得焦點。我已經通過內核和@Cedrick走了兩條路線。 –
是的,我爲UIImageView做了類似的事情,我可以將它聚焦(即使它不在表格或collectionview中),但我無法使TextView工作。不知道爲什麼現在,因爲我已經做了上面提到的所有事情 – c0d3Junk13