0

I am trying to set up a button using UIControlEventTouchDragEnter as the way to trigger the button's method. Specifically, I have a button, and I want the button's method to be triggered if the user presses their finger outside of the button, and drags their finger into the bounds of the button.

According to apple, this event, UIControlEventTouchDragEnter, is: An event where a finger is dragged into the bounds of the control.

However, I can't get the button to trigger. Here is my code:

- (IBAction)touchDragEnter:(UIButton *)sender {
    _samlpe.image = [UIImage imageNamed:@"alternate_pic.png"];
}

So, when touchInto for this button is triggered, the method will change the current image of _sample into this alternate image. If I just use touchUpInside, the image does change to the alternate upon button click.

Does anyone know why this isn't working, or have work-arounds? Thanks!

jake9115
  • 3,964
  • 12
  • 49
  • 78

1 Answers1

4

The touchDragEnter is only triggered when you initially tap the button, drag your finger to the outside of the bounds of the button, and drag again into the bounds of the button.

You might want to make use of touchesMoved method in your view controller class and detect the button entered based on the location of the touch:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch * touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:touch.view];
    NSLog(@"%f - %f", touchLocation.x, touchLocation.y);
}
Valent Richie
  • 5,226
  • 1
  • 20
  • 21
  • That is fantastic, thanks for the great answer. I also put an if statement within your method to detect if the touches are happening within a certain area of the screen. Upon touches being found in that area, I want a 1 second audio clip to be played. However, if you drag your finger around, it keeps validated the if statement, and the sound keep restarting after every pixel movement. Is there a way to start the sound and let it finish without starting again? – jake9115 May 21 '13 at 03:42
  • @jake9115: I would suggest you open a new question for the sound complete with the code for the sound and the if statement :) – Valent Richie May 21 '13 at 03:46