Rakesh
Rakesh

Reputation: 302

change the color of uiview when pressed/released

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 ?

Upvotes: 1

Views: 1365

Answers (1)

Natarajan
Natarajan

Reputation: 3271

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!

Upvotes: 2

Related Questions