0

I need to change UIButton image to the image of "selected" or "highlighted" state on tap down (not on the end of tap).

Also I need to change a state of the button to "selected" on tap with the code:

- (IBAction)convertDown:(id)sender {
    [buttonConvert setSelected:TRUE];
}

But with this code I see the image of the "normal" state until end of the tap:(

How to fix this?

Dmitry
  • 14,306
  • 23
  • 105
  • 189

2 Answers2

0

It's Interface Builder issue. Can't set an image for that period of time until the end of the tap. Can be fixed by the following code:

- (void)viewDidLoad {
    [super viewDidLoad];
    [buttonConvert setBackgroundImage:[UIImage imageNamed:@"iphone_button_highlighted.png"] 
        forState:UIControlStateSelected | UIControlStateHighlighted];
}
Dmitry
  • 14,306
  • 23
  • 105
  • 189
  • Can also do that in Interface Builder. And it's the same as what I said. – matt Jan 05 '14 at 18:12
  • There is no `UIControlStateSelected | UIControlStateHighlighted` state on the Interface Builder. – Dmitry Jan 05 '14 at 18:26
  • Okay, I take it back! I'm just going to stand back and let this sort itself out. – matt Jan 05 '14 at 18:30
  • @Altaveron : there is (_just not together like that_). check this answer: [link](http://stackoverflow.com/a/16841071/2857130) – staticVoidMan Jan 05 '14 at 18:50
  • It 100% doesn't work - I've checked it. – Dmitry Jan 05 '14 at 19:04
  • @Altaveron : well, maybe you're trying something else altogether in the first place and instead need something like **`UIControlEventTouchDown`** instead of **`UIControlEventTouchUpInside`**. example: **`[yourButtonObject addTarget:self action:@selector(convertDown:) forControlEvents:UIControlEventTouchDown];`** this will run the method **`convertDown:`** on "_tap down_" and will not wait for the "_end of tap_" – staticVoidMan Jan 06 '14 at 14:26
  • It's totally unrelated. Standard delegate works fine on tap down but it shows incorrect image of the button. – Dmitry Jan 06 '14 at 18:15
0

Assign diffrent targets for your button ControlEvents

There are many ControlEvents available:

   UIControlEventTouchDown           = 1 <<  0,
   UIControlEventTouchDownRepeat     = 1 <<  1,
   UIControlEventTouchDragInside     = 1 <<  2,
   UIControlEventTouchDragOutside    = 1 <<  3,
   UIControlEventTouchDragEnter      = 1 <<  4,
   UIControlEventTouchDragExit       = 1 <<  5,
   UIControlEventTouchUpInside       = 1 <<  6,
   UIControlEventTouchUpOutside      = 1 <<  7,
   UIControlEventTouchCancel         = 1 <<  8,

   UIControlEventValueChanged        = 1 << 12,

   UIControlEventEditingDidBegin     = 1 << 16,
   UIControlEventEditingChanged      = 1 << 17,
   UIControlEventEditingDidEnd       = 1 << 18,
   UIControlEventEditingDidEndOnExit = 1 << 19,

   UIControlEventAllTouchEvents      = 0x00000FFF,
   UIControlEventAllEditingEvents    = 0x000F0000,
   UIControlEventApplicationReserved = 0x0F000000,
   UIControlEventSystemReserved      = 0xF0000000,
   UIControlEventAllEvents           = 0xFFFFFFFF

Example:

[yourButton addTarget:self 
           action:@selector(methodTouchUpInside:)
 forControlEvents: UIControlEventTouchUpInside];

- (void)methodTouchDown:(id)sender{

   NSLog(@"TouchDown");
}

or Add gustures instead of assigning targets for ControlEvents

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[yourButton addGestureRecognizer: tapGesture];

- (void) handleTap:(UITapGestureRecognizer *)sender
{
  if(sender.state == UIGestureRecognizerStateBegan)
  {
    NSLog(@"UIGestureRecognizerStateBegan");
  }
  else if(sender.state == UIGestureRecognizerStateEnded)
  {
    NSLog(@"UIGestureRecognizerStateEnded");
  }
}

There are many UIGestureRecognizerState available.

   UIGestureRecognizerStatePossible,
   UIGestureRecognizerStateBegan,
   UIGestureRecognizerStateChanged,
   UIGestureRecognizerStateEnded,
   UIGestureRecognizerStateCancelled,
   UIGestureRecognizerStateFailed,
   UIGestureRecognizerStateRecognized
Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102