2015-11-23 32 views
5

我試圖實現6行高描述標籤,我希望它是可以聚焦的。理想情況下,這意味着將UILabel類擴展爲自定義組件。我試圖通過實施canBecomeFocused和​​,但我的UILabel似乎沒有得到任何關注。使UILabel可調焦和可點擊(tvOS)

我也嘗試用UIButton替換UILabel,但按鈕並沒有真正針對這類事情進行優化。此外,這將意味着我需要更改buttonType關注焦點從自定義到平原..和buttonType似乎是一個現成的財產。

實際上,我希望蘋果在Apple TV Movies應用程序中使用完全相同的文本標籤。對於電影描述他們有一個文本標籤,顯示幾行文字和一個「更多」。聚焦時,它看起來像一個按鈕(陰影周圍)和改變背景顏色。點擊時 - 它會打開一個帶有完整電影描述的模式窗口。

enter image description here

有什麼建議?或者也許有人已經實現了這個自定義控制tvOS?或更好的事件 - 蘋果有一個組件可以做到這一點,我錯過了一些東西。

PS:雨燕的解決辦法是歡迎的。

回答

5

好吧,回答我的問題:)

所以顯得有些有些意見是「可聚焦」上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) 
     } 
    } 

} 
+0

感謝這個答案,我修改了一下我的項目。 https://gist.github.com/Cedrick84/8922a0af730c39c05794 – Cedrick

+0

最近有什麼改變與tvOS會導致這不起作用?無論我嘗試什麼,我仍無法獲得TextView來獲得焦點。我已經通過內核和@Cedrick走了兩條路線。 –

+0

是的,我爲UIImageView做了類似的事情,我可以將它聚焦(即使它不在表格或collectionview中),但我無法使TextView工作。不知道爲什麼現在,因爲我已經做了上面提到的所有事情 – c0d3Junk13

1

使用集合視圖只有一個單元,並添加轉化細胞和細胞更改背景顏色didUpdateFocusInContext焦點移動到小區時。

override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) { 
    coordinator.addCoordinatedAnimations({ 
     if self.focused { 
      self.transform = CGAffineTransformMakeScale(1.01, 1.01) 
      self.backgroundColor = UIColor.whiteColor() 
      self.textLabel.textColor = .blackColor() 
     } 
     else { 
      self.transform = CGAffineTransformMakeScale(1, 1) 
      self.backgroundColor = UIColor.clearColor() 
      self.textLabel.textColor = .whiteColor() 
     } 
     }, completion: nil) 
} 

作爲一個額外的步驟,你可以嘗試提取圖像的顏色,如果你使用的是圖像的iTunes一樣的背景和使用,對於小區後面視覺效果圖。

你也可以申請轉換到的CollectionView的視頻控制器,使它看起來像焦點

-1

可以使用系統按鈕,然後在故事板的背景圖片設置爲包含你喜歡的顏色的圖像

+0

他想使用UILabel或UITextView。使用系統按鈕是一個可怕的解決方案。 –