ios - 按下/释放时更改 uiview 的颜色

标签 ios swift uiview

有没有办法在按下和释放时更改 uiview 的颜色。

我尝试使用 touchesBegan 和 touchesEnded 覆盖它,但它影响了现有功能。那么我们能否在不影响现有功能的情况下实现它?

最佳答案

您可以使用 UIGestureRecognizer 检测 View 上的触摸事件。请引用以下链接了解更多信息。

https://www.raywenderlich.com/433-uigesturerecognizer-tutorial-getting-started https://developer.apple.com/documentation/uikit/uipangesturerecognizer

例子:

let myView = UIView()
let gesture = UITapGestureRecognizer(target: self, action: #selector(tapEventDetected(gesture:)))
myView.addGestureRecognizer(gesture)

@objc func tapEventDetected(gesture:UITapGestureRecognizer){

        if gesture.state == .began{

            //Set touch began color
        }

        if gesture.state = .ended{

            //Set touch ended color
        }
}

谢谢!

关于ios - 按下/释放时更改 uiview 的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53441789/

相关文章:

Swift 的 JSONDecoder 在 JSON 字符串中具有多种日期格式?

ios - 如何使用带有 Storyboard 的 Autolayout 获取 UIView 子类的框架?

ios - PaintCode 中的 UIButton (Swift)

ios - 使用协议(protocol)检测 UISegmentedControl 中的值变化

ios - HKObserverQueryCompletionHandler 超时?

ios - Nil 与预期的字典值类型 'Any' swift 不兼容

ios - 我无法在任何地方访问数组对象,如何快速访问它们?

ios - 'NSInternalInconsistencyException',原因 : 'Requested the number of rows for section (0) which is out of bounds.'

ios - 在 iPhone 上处于横向模式时,如何为 UINavigationBar 的背景使用不同的图像?

iphone - 使用 UIButton 按下传递对象数据