Keeping a Uibutton Selected After a Touch

Keep UIButton Selected/Highlighted after touch

Use below code
declare isHighLighted as instance variable

//write this in your class
var isHighLighted:Bool = false


override func viewDidLoad() {

let button = UIButton(type: .system)

button.setTitle("Your title", forState: UIControlState.Normal)
button.frame = CGRectMake(0, 0, 100, 44)

self.view.addSubview(button as UIView)

button.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)

}

func buttonClicked(sender:UIButton)
{
dispatch_async(dispatch_get_main_queue(), {

if isHighLighted == false{
sender.highlighted = true;
isHighLighted = true
}else{
sender.highlighted = false;
isHighLighted = false
}
});
}

I would recomend to use selected state instead of highlighted the below code demonstarate with selected state

override func viewDidLoad() {

let button = UIButton(type: .system)

button.setTitle("Your title", forState: UIControlState.Normal)
button.frame = CGRectMake(0, 0, 100, 44)

self.view.addSubview(button as UIView)
//set normal image
button.setImage(normalImage, forState: UIControlState.Normal)
//set highlighted image
button.setImage(selectedImage, forState: UIControlState.Selected)

button.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)

}

func buttonClicked(sender:UIButton)
{
sender.selected = !sender.selected;
}

Keeping a UIButton selected after a touch

How are you setting the images for the different UIControlStates on the button? Are you setting a background image for UIControlStateHighlighted as well as UIControlStateSelected?

UIImage *someImage = [UIImage imageNamed:@"SomeResource.png"];
[button setBackgroundImage:someImage forState:UIControlStateHighlighted];
[button setBackgroundImage:someImage forState:UIControlStateSelected];

If you're setting the selected state on the button touch down event rather than touch up inside, your button will actually be in a highlighted+selected state, so you'll want to set that too.

[button setBackgroundImage:someImage forState:(UIControlStateHighlighted|UIControlStateSelected)];

Edit:

To sum up my remarks in the comments and to address the code you posted...you need to set your background images for the full UIControl state that you're in. According to your code snippet, this control state would be disabled + selected + highlighted for the duration of the network operation. This means that you would need to do this:

[button setBackgroundImage:someImage forState:(UIControlStateDisabled|UIControlStateHighlighted|UIControlStateSelected)];

If you remove the highlighted = YES, then you would need this:

[button setBackgroundImage:someImage forState:(UIControlStateDisabled|UIControlStateSelected)];

Get the picture?

How to keep UIButton highlighted until a second touch?

I will use selected state instead of highlighted. UIButton has already the property so you don't need to create any other property.

button.setImage(image, forState: UIControlState.Normal)
button.setImage(selectedImage, forState: UIControlState.Selected)

button.addTarget(self, action: "buttonTapped:", forControlEvents: UIControlEvents.TouchUpInside)


func buttonTapped(sender:UIButton)
{
sender.selected = !sender.selected;
}

UIButton stay selected after Touch

The reason for the flash is that Touch Up Inside is too late; the user's finger is already up, by definition. Perhaps you should consider implementing an action event for Touch Down Inside instead (or in addition).

Another possibility might be to use a UISegmentedControl with one segment and momentary set to NO.

keeping a UIButton pressed(state selected/highlighted),until another button is pressed?

What you did is set an image for the "Highlighted" state, this is why when you push it you can see your image.

What you want to do is

1) set the image for the SELECTED state

2) create a property for your view controller (just controll drag the button to the header) using the assistant view (while on storyboard, second square on the top right)

3) on your method for the button's action type:

button.selected = !button.selected;

(obviously replace button to whatever you named your property to)

Why is the selected state of my UIButton not triggered before I release my finger?

You probably want to change the image for the highlighted state.

You shouldn't need to do anything in the touch: method.

Just change the line:

[button setImage:[UIImage imageNamed:@"touch_selected.png"] forState:UIControlStateSelected];

for:

[button setImage:[UIImage imageNamed:@"touch_selected.png"] forState:UIControlStateHighlighted];

Then the image should change when the button is touched.

EDIT:

To have the button keep the image once the touch has been released, you essentially want to invert the selected state each time a touch is released on the button.
I suggest adding a target to the button:

[myButton addTarget:self action:@selector(buttonPushed) forControlEvent:UIControlEventTouchUpInside];

Then in that method, invert the selected state:

- (void)buttonPushed
{
[myButton setSelected:!myButton.selected];
}

That should hopefully do the trick!



Related Topics



Leave a reply



Submit