2

所以这是一个 Swift 中的 tvOS 项目。我有一个带有按钮的自定义 UICollectionViewCell 作为其子视图之一。我向按钮添加了一个目标,以允许它解释点击。这是相关代码的简化版本

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”添加到单元格类中以查看它是否有任何内容并被调用

func pressesEnded(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
        // If I put a breakpoint here it's reached
}

有什么建议么?当我在模拟器中运行它时,按钮可以获得焦点,所以我不知道为什么它无法获得任何事件

4

4 回答 4

3

所以,我想出了如何解决这个问题,尽管它是一种解决方法。基本上UICollectionView我需要确保单元格无法获得焦点。

接下来我之前有didUpdateFocusInContextCustomCell。这实际上是在cell时为按钮设置动画,但是当我检查按钮时从未获得焦点。我猜这是在拦截它。所以我从文件中删除了该函数CustomCell,而是在文件的底部添加了该函数作为 UIButton 的扩展。

这也可以通过创建一个子类UIButton并使用它来完成,但这样代码更少(这可能是理想的方式)。所以完整的代码如下:

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
            }
        }
    }
}
于 2016-01-12T19:23:37.710 回答
0

确保选中 UICollectionViewCell 的User Interaction Enabled复选框。

在此处输入图像描述

于 2017-04-14T17:11:11.643 回答
-2

上面的答案是正确的,如果您使用自定义UICollectionViewCell,您也可以在其子类中为特定单元格执行此操作:

override func canBecomeFocused() -> Bool {
    return false
}
于 2016-07-26T11:21:58.587 回答
-2

似乎 UICollectionViewCell 被一个清晰的视图自动覆盖,它可以捕捉水龙头。

对我们来说,只需调用 bringSubview(toFront: checkButton) 就可以了。现在应该调用 checkButton touchUpInside 操作。

import UIKit

class SerieItemCollectionViewCell: UICollectionViewCell {

   var checked : (()->())?


   static let reuseIdentifier = "SerieItemCollectionViewCell"

   @IBOutlet weak var checkButton: UIButton!
   @IBOutlet var imageView: UIImageView!

   override func layoutSubviews() {
       super.layoutSubviews()
       bringSubview(toFront: checkButton)
   }

   @IBAction func buttonClicked(_ sender: Any) {
       checked?()
       print("Hello")
   }
}
于 2017-02-03T15:48:40.053 回答