Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使UIButton按键在Swift的UICollectionViewCell中工作?_Swift_Uibutton_Uicollectionviewcell_Tvos_Uicontrolevents - Fatal编程技术网

如何使UIButton按键在Swift的UICollectionViewCell中工作?

如何使UIButton按键在Swift的UICollectionViewCell中工作?,swift,uibutton,uicollectionviewcell,tvos,uicontrolevents,Swift,Uibutton,Uicollectionviewcell,Tvos,Uicontrolevents,这是Swift中的一个tvOS项目。我有一个自定义UICollectionViewCell,其中一个子视图是一个按钮。我向按钮添加了一个目标,以允许它解释点击。下面是相关代码的简化版本 class CustomCell: UICollectionViewCell { var button:UIButton! override init(frame: CGRect) { super.init(frame: frame) button = UIBu

这是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!")
    }
}
由于某种原因,它从来没有打印出我的信息。我尝试将“Presseeded”添加到cell类中,以查看它是否得到任何调用

func pressesEnded(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
        // If I put a breakpoint here it's reached
}
func PRESSESEND(按:设置,带事件:UIPressesEvent?){
//如果我在这里放一个断点,它就到达了
}

有什么建议吗?当我在模拟器中运行它时,按钮可以获得焦点,所以我不知道为什么它不能获得任何事件,所以,我找到了解决方法,尽管这是一种解决方法。基本上,对于
UICollectionView
我需要确保单元格不能获得焦点

接下来,我在之前的
CustomCell
中更新了
didUpdateFocusInContext
。这是当细胞移动时按钮的动画,但当我选中按钮时,按钮从未获得焦点。我猜这是在拦截它。因此,我从
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
            }
        }
    }
}

所以,我找到了解决这个问题的方法,虽然这是一个解决办法。基本上,对于
UICollectionView
我需要确保单元格不能获得焦点

接下来,我在之前的
CustomCell
中更新了
didUpdateFocusInContext
。这是当细胞移动时按钮的动画,但当我选中按钮时,按钮从未获得焦点。我猜这是在拦截它。因此,我从
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
            }
        }
    }
}

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

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

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

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

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")
   }
}

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")
   }
}
确保选中UICollectionViewCell的“已启用用户交互”复选框

确保选中UICollectionViewCell的“已启用用户交互”复选框


在我的例子中,collectionViewCell中的视图中有一个按钮。发现在xib中,userInteractionEnabled已关闭。Omg)在我的例子中,collectionViewCell中的视图中有一个按钮。发现在xib中,userInteractionEnabled已关闭。天哪