2015-11-23 34 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。使用系统按钮是一个可怕的解决方案。 –