【问题标题】:UIButton not going changing text color when there is long-press gesture recognizer当有长按手势识别器时,UIButton 不会改变文本颜色
【发布时间】:2016-09-29 20:45:47
【问题描述】:

我有以下代码:

deleteAllButton.setTitle("Delete", forState: .Normal)
        deleteAllButton.titleLabel?.numberOfLines = 2
        deleteAllButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
        deleteAllButton.titleLabel?.font = UIFont.systemFontOfSize(keyboardFontSize)
    //  deleteAllButton.addTarget(self, action: #selector(deletekeyPressed), forControlEvents: .TouchUpInside)

        let deleteLPGR = UILongPressGestureRecognizer(target: self, action: #selector(deleteButtonLongPress))
        deleteLPGR.minimumPressDuration = 0
        deleteLPGR.delaysTouchesBegan = true

        deleteAllButton.addGestureRecognizer(deleteLPGR)

通常当按下 UIButton 时,文本会自动更改颜色以指示它处于“按下”状态。但是,由于我添加了长按手势识别器,因此该按钮并不表示它处于“按下”状态。

我怎样才能让触摸事件通过按钮,让用户感觉按钮实际上被“按下”了?

【问题讨论】:

    标签: ios swift uibutton long-press uilongpressgesturerecogni


    【解决方案1】:

    我认为您不需要添加手势识别器。

    只需尝试为高亮状态设置标题颜色,它应该可以工作

    deleteAllButton.setTitleColor(UIColor.redColor(), forState: .Highlighted)
    

    希望这能解决您的问题。

    【讨论】:

      【解决方案2】:

      只要把deleteLPGR.minimumPressDuration = 0改成deleteLPGR.minimumPressDuration = 1就可以看到颜色变化了

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-09
        • 1970-01-01
        • 1970-01-01
        • 2015-03-19
        相关资源
        最近更新 更多