问题描述
所以这是一个 Swift 的 tvOS 项目.我有一个自定义 UICollectionViewCell ,带有一个按钮作为其子视图之一.我向按钮添加了一个目标以允许它解释点击.下面是相关代码的简化版
So this is a tvOS project in Swift. I have a custom UICollectionViewCell with a button as one of its subviews. I add a target to the button to allow it to interpret clicks. Here's a simplified version of the relevant code
class CustomCell: UICollectionViewCell {
var button:UIButton!
override init(frame: CGRect) {
super.init(frame: frame)
button = UIButton(...) // Button is initialized with a frame
button.userInteractionEnabled = true
button.enabled = true
button.addTarget(self, action: "pressed:", forControlEvents: .PrimaryActionTriggered)
contentView.addSubview(button)
}
func pressed(sender: UIButton!) {
print("button pressed!")
}
}
出于某种原因,它从未打印出我的消息.我尝试将pressedEnded"添加到单元格类中,以查看它是否有任何内容并被调用
For some reason it's not ever printing out my message. I tried adding 'pressedEnded' to the cell class to see if it's getting any and it gets called
func pressesEnded(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
// If I put a breakpoint here it's reached
}
有什么建议吗?当我在模拟器中运行它时按钮可以获得焦点,所以我不知道为什么它无法获得任何事件
Any suggestions? When I run it in the simulator the button can get focus, so I don't know why it can't get any events
推荐答案
所以,我想出了如何解决这个问题,尽管它在某种程度上是一种解决方法.基本上对于 UICollectionView
我需要确保单元格的不能获得焦点.
So, I figured out how to solve this, although it's somewhat of a workaround. Basically for the UICollectionView
I need to ensure the cell's can't get focus.
接下来,我之前在 CustomCell
中有 didUpdateFocusInContext
.这就是 cell 时按钮的实际动画效果,但是当我检查按钮时,按钮从未获得焦点.我猜这是拦截它.因此,我从 CustomCell
中删除了该函数,而是在文件底部添加了该函数作为 UIButton 的扩展.
Next I had didUpdateFocusInContext
in CustomCell
previously. This was what was actually animating the button when the cell, but when I checked the button never got focus. I'm guessing this was intercepting it. So I removed that function from CustomCell
and instead, at the bottom of my file I added that function as an extension of UIButton.
这也可以通过创建 UIButton
的子类并使用它来完成,但代码较少(这可能是理想的方式).所以完整的代码看起来像:
This also could've been done by creating a subclass of UIButton
and using that instead, but this was less code (that is probably the ideal way). So the full code looks like:
class MyCollection: UICollectionView, UICollectionViewDelegate {
// Need initializer functions as well as functions for creating CustomCell's. They're omitted because they're not relevant to the answer
func collectionView(collectionView: UICollectionView, canFocusItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return false
}
}
class CustomCell: UICollectionViewCell {
var button:UIButton!
override init(frame: CGRect) {
super.init(frame: frame)
button = UIButton(...) // Button is initialized with a frame
button.userInteractionEnabled = true
button.enabled = true
button.addTarget(self, action: "pressed:", forControlEvents: .PrimaryActionTriggered)
self.addSubview(button)
}
func pressed(sender: UIButton!) {
print("button pressed!")
}
}
extension UIButton {
override public func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
super.didUpdateFocusInContext(context, withAnimationCoordinator: coordinator)
if self.superview is CustomCell { // This ensures that all UIButtons aren't affected
if context.nextFocusedView == self {
// Perform actions for when UIButton is focused
}else {
// Perform actions for when UIButton loses focus
}
}
}
}
这篇关于如何让 UIButton 按下以在 Swift 中的 UICollectionViewCell 中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!