3

I'm new to Swift and I'm trying to change the button colour on tap. When the button is tapped, it should change colour and when released it should go back to the original button colour.

An example of this is the calculator. When you tap a button, it changes from light grey to dark grey and when the user removes their finger off the button, it goes back to the original light grey colour. How do I go about doing that?

So far, I've only got this..

@IBAction func changeButtonColourOnTouch(sender: UIButton) {

    zeroButton.backgroundColor = UIColor.blueColor()

}

The above code changes the button colour to blue but stays blue.

daanyyaal
  • 301
  • 2
  • 4
  • 16
  • You can use this [answer](http://stackoverflow.com/a/30604658/5812592) by winterized. – Tada Mar 01 '16 at 20:10

5 Answers5

5

The problem is, after releasing the button, you should return the color to the original state.

You can link direct from storyboard, in the action TouchUpInside (release) and TouchDown (press) , to change the button color to the correct state in each event.

enter image description here

Or you can add a Target in the button by code, linking to the functions, as the code bellow shows it.

    zeroButton.addTarget(self, action: Selector("holdRelease:"), forControlEvents: UIControlEvents.TouchUpInside);
    zeroButton.addTarget(self, action: Selector("HoldDown:"), forControlEvents: UIControlEvents.TouchDown)

    //target functions   
    func HoldDown(sender:UIButton)
    {
    zeroButton.backgroundColor = UIColor.blueColor()
    }

    func holdRelease(sender:UIButton)
    {
    zeroButton.backgroundColor = UIColor.whiteColor()
    }

Code adapted by the present in the link UIButton with hold down action and release action

Community
  • 1
  • 1
Ulysses
  • 2,281
  • 1
  • 16
  • 24
3

The checked answer above works, but if the user holds down on the button, then drags out, the background color won't return to normal. It's a tiny UI bug, but a simple fix. This includes the code of the checked answer.

    zeroButton.addTarget(self, action: #selector(holdRelease), for: .touchUpInside);
zeroButton.addTarget(self, action: #selector(heldDown), for: .touchDown)
 zeroButton.addTarget(self, action: #selector(buttonHeldAndReleased), for: .touchDragExit)

//target functions   
@objc func heldDown()
{
zeroButton.backgroundColor = .blue
}

@objc func holdRelease()
{
zeroButton.backgroundColor = .white
}

@objc func buttonHeldAndReleased(){
    zeroButton.backgroundColor = .blue
}
Eric
  • 199
  • 1
  • 11
  • Could you specify the answer you refer to more accurately, and provide a complete answer? Otherwise, if the answer you're referring to changes your answer changes its meaning too, or worse, becomes outright misleading. – glennsl Aug 29 '18 at 15:48
1

you could also use a tap gesture and change the color of the button as the user is tapping on the button

you could see apple documentation regarding UIGestureRecognizer in here

it is a little bit advanced, but you will learn a lot from it.

Community
  • 1
  • 1
Ali Alebrahim
  • 617
  • 1
  • 9
  • 25
1

You can also use setbackgroundImageForState to define color image as button background for all UIControlState you are interested in e.g. Normal, Highlighted, Disabled, Selected.

let cx = UIGraphicsGetCurrentContext()
let color = UIColor.redColor()
let state = UIControlState.Selected

UIGraphicsBeginImageContext(CGSize(width:1, height:1))
CGContextSetFillColorWithColor(cx, color.CGColor)
CGContextFillRect(cx, CGRect(x:0, y:0, width:1, height:1))
let colorImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

self.zeroButton.setBackgroundImage(colorImage, forState: state)

Now color changes automatically and you don't have to handle it manually.

JOM
  • 8,139
  • 6
  • 78
  • 111
1

If you want that for a programmatically defined button, just declare the button of type system during its initialisation:

 let button = UIButton(type: .system)
Ronit Mankad
  • 117
  • 1
  • 7