【问题标题】:Subclassing UIBarButtonItem with touchesBegan action使用 touchesBegan 操作子类化 UIBarButtonItem
【发布时间】:2014-09-05 06:13:38
【问题描述】:

我正在尝试创建一个 UIBarButtonItem 的子类,我可以在 Storyboards/Xibs 中使用该子类,其中可以收听 touchesBegan 动作(在触摸按钮时播放按钮声音)。这是我得到的:

class UIAudibleBarButtonItem : UIBarButtonItem {
    override init()     {
        super.init()
        target = self
        action = "touchesBegan:"
    }

    required init(coder aDecoder:NSCoder) {
        super.init(coder: aDecoder)
    }

    func touchesBegan(touches:NSSet, withEvent event:UIEvent) {
        AudioManager.instance.playButtonSound()
    }
}

但是它不起作用,即不调用 touchesBegan。我需要进行哪些更改才能完成这项工作?

更新:

class UIAudibleBarButtonItem : UIBarButtonItem {
    override init() {
        super.init();
        setup();
    }

    override init(barButtonSystemItem systemItem: UIBarButtonSystemItem, target: AnyObject?, action: Selector) {
        super.init(barButtonSystemItem: systemItem, target: target, action: action);
        setup();
    }

    override init(image: UIImage?, landscapeImagePhone: UIImage?, style: UIBarButtonItemStyle, target: AnyObject?, action: Selector) {
        super.init(image: image, landscapeImagePhone: landscapeImagePhone, style: style, target: target, action: action);
        setup();
    }

    override init(image: UIImage?, style: UIBarButtonItemStyle, target: AnyObject?, action: Selector) {
        super.init(image: image, style: style, target: target, action: action);
        setup();
    }


    override init(title: String?, style: UIBarButtonItemStyle, target: AnyObject?, action: Selector) {
        super.init(title: title, style: style, target: target, action: action);
        setup();
    }

    override init(customView:UIView) {
        super.init(customView: customView);
        setup();
    }

    required init(coder aDecoder:NSCoder) {
        super.init(coder: aDecoder);
        setup();
    }


    private func setup()
    {
        target = self;
        action = "buttonPressed:";
        println("setup()");
    }


    func buttonPressed(sender:AnyObject)
    {
        AudioManager.instance.playButtonSound();
        println("buttonPressed()");
    }
}

【问题讨论】:

  • Hitest 并检查哪个视图正在处理触摸。

标签: ios iphone cocoa-touch swift uibarbuttonitem


【解决方案1】:

如果您使用 swift,这里有一个简单的方法来检测下面的操作。您所要做的就是在情节提要中连接该类,单击条形按钮项,您将看到动作触发。

稍后谢谢我!!!

class MVBarbuttonItem: UIBarButtonItem {

    override func awakeFromNib() {
        self.action = #selector(MVBarbuttonItem.didTapButton)
    }

    @objc func didTapButton() {

    }

}

【讨论】:

    【解决方案2】:

    我假设您将 UIAudibleBarButtonItem 作为故事板中的自定义类提供,而不是将这一行放在您的类 @objc(UIAudibleBarButtonItem) 的正上方,然后查看它是否有效。

    这对我有用。

    【讨论】:

      【解决方案3】:

      UIBarButtonItem 不是UIView

      如果您想实现这样的目标,请创建一个按钮,聆听对该按钮的触摸(使用 addTarget:...touchesBegan )并使用 initWithCustomView 从该按钮创建您的栏按钮项目。

      编辑:

      根据确切播放声音的时间,还有另一种可能性。如果您想在按下和释放后播放声音,只需将上面的代码修改为:

      action = "buttonPressed:" ... func buttonPressed(sender:AnyObject)

      【讨论】:

      • 但这只能以编程方式工作吗?不适用于 Storyboards/Xib 文件,对吧?
      • @Avalon 看到我的编辑。如果您需要在用户触摸按钮时立即播放声音,您需要做的很困难。
      • 感谢您的编辑,但它不起作用。 buttonPressed() 没有被调用。除了处理程序名称不同之外,这与我上面的代码有何不同?编辑:实际上初始化程序根本没有被调用。但是编码器初始化被调用了!但是即使将这两行放入coder init中,它也不起作用。
      • 差别不大,只有正确的参数个数。 action = "buttonPressed:" 行是否执行?编辑:然后将这两个目标和操作行也放在另一个初始化中(或创建一个 commonInit 函数并从两个初始化中调用它)
      • 好的,我重写了所有的初始化器,只是为了确保当我测试它时,通用的 init 方法肯定会被调用。但是 buttonPressed 永远不会被调用。另请参阅上面的更新代码。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 2012-10-03
      • 1970-01-01
      相关资源
      最近更新 更多