0

Is there a way to change color of the uiview when pressed and released.

I tried it using touchesBegan and touchesEnded by overriding it but it effected the existing functionality. so without effecting the existing functionality can we achieve it ?

Rakesh
  • 282
  • 3
  • 17

1 Answers1

2

You can use UIGestureRecognizer to detect touch events on your view. Please refer below links for more info.

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

Example:

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

Thanks!

Natarajan
  • 3,155
  • 3
  • 16
  • 34
  • I had tried using the touches began and touches ended method but it had impacted the original functionality – Rakesh Nov 23 '18 at 07:02
  • Okay, So that I recommended you to use UIGestureRecognizer. Please see example code in my edited answer. – Natarajan Nov 23 '18 at 07:16