Reputation: 465
I have a button that should move 2 pixels down when pressed down and jump back to the original location when the press ended. I've tried touchesBegan
and touchesEnded
but those don't work with a button. I've also tried exchanging the button with an IUView and calling the button's method in touchesEnded
but that gave the button a weird delay before moving down. Any ideas?
Upvotes: 1
Views: 2875
Reputation: 490
Did you try using the Touch Down and Touch Up events? Try moving the button location on Touch Down and restoring it to its initial value on Touch Up (Inside and Outside).
EDIT: Here's a code example:
- (IBAction)moveButtonDown:(id)sender
{
UIButton *btn = (UIButton *)sender;
btn.frame = CGRectMake(btn.frame.origin.x, btn.frame.origin.y + 2, btn.frame.size.width, btn.frame.size.height);
}
- (IBAction)moveButtonUp:(id)sender
{
UIButton *btn = (UIButton *)sender;
btn.frame = CGRectMake(btn.frame.origin.x, btn.frame.origin.y - 2, btn.frame.size.width, btn.frame.size.height);
}
Assign moveButtonDown: to the buttons "Touch Down" event and moveButtonUp: to both its "Touch Up Inside" and "Touch Up Outside" events. Tested and working, here at least ;)
EDIT: Thanks fichek, here's a solution with CGRectOffset instead of CGRectMake:
- (IBAction)moveButtonDown:(id)sender
{
UIButton *btn = (UIButton *)sender;
btn.frame = CGRectOffset(btn.frame, 0, 2);
}
- (IBAction)moveButtonUp:(id)sender
{
UIButton *btn = (UIButton *)sender;
btn.frame = CGRectOffset(btn.frame, 0, -2);
}
Upvotes: 1