如何让UIButton印刷机在Swift中的UICollectionViewCell中工作?

时间:2016-01-12 17:34:42

标签: swift uibutton uicollectionviewcell tvos uicontrolevents

所以这是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 个答案:

答案 0 :(得分:3)

所以,我想出了如何解决这个问题,虽然它有点像解决方法。基本上对于UICollectionView我需要确保单元格无法获得焦点。

接下来我在didUpdateFocusInContext之前获得了CustomCell。这是单元格时实际动画按钮的功能,但是当我检查按钮时,从未获得焦点。我猜这是在拦截它。所以我从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
            }
        }
    }
}

答案 1 :(得分:0)

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

enter image description here

答案 2 :(得分:-2)

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

override func canBecomeFocused() -> Bool {
    return false
}

答案 3 :(得分:-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")
   }
}