1

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
  • 302
  • 3
  • 19

1 Answers1

1

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,241
  • 3
  • 17
  • 34