Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Touch drag enter works?

Can anyone give me an example about Touch drag enter to drag from a button to another that triggering both button's event. And how does it work?

For example, I want to drag from Do to Fa that event of Do, Re, Mi, Fa are triggered.

iOS Simulator Screenshot

Here is my code:

- (void) setupVC {
soundBankPlayer = [[SoundBankPlayer alloc] init];
[soundBankPlayer setSoundBank:@"Piano"];
arrMusicalNotes = [NSArray arrayWithObjects:@"60", @"62", @"64", @"65", @"67", @"69", @"71", @"72", nil];
}


#pragma mark - Setup Musical Note
- (IBAction)btnMusicalNoteclick:(id)sender {
    int numOfNote = [[arrMusicalNotes objectAtIndex:((UIButton*)sender).tag] intValue];
    NSLog(@"%i", numOfNote);
    [soundBankPlayer queueNote:numOfNote gain:1.0f];
    [soundBankPlayer playQueuedNotes];
}

- (IBAction)btnDragOut:(id)sender {
    NSLog(@"Out");
}

Oh I've seen that when i hold click out of Simulator, the method btnDragOut is triggered. And when i drag from out of Simulator to the button, the method of this button is triggered. Now I want the method btnDragOut is triggered when i drag out of a button (finger is still in Simulator). Anyone know that?

like image 855
erictruong Avatar asked Aug 10 '15 10:08

erictruong


1 Answers

You can add UIPanGestureRecognizer to your view of your UIViewController subclass via Storyboard or via code in viewDidLoad method:

UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleDrag:)];
[self.view addGestureRecognizer:gestureRecognizer];

Then you can add property in implementation file of your UIViewController subclass of current UIButton being dragged:

@interface YourViewController ()
@property (weak, nonatomic) UIButton *currentButton;
@end

Now in action method you can detect UIControlEventTouchDragEnter and UIControlEventTouchDragExit events as follows:

- (void)handleDrag:(UIPanGestureRecognizer *)gestureRecognizer
{
    CGPoint point = [gestureRecognizer locationInView:self.view];
    UIView *draggedView = [self.view hitTest:point withEvent:nil];

    if (gestureRecognizer.state == UIGestureRecognizerStateChanged) {
        if ([draggedView isKindOfClass:[UIButton class]] && !self.currentButton) {
            self.currentButton = (UIButton *)draggedView;
            NSLog(@"Enter: %ld", (long)self.currentButton.tag);

            // send enter event to your button
            [self.currentButton sendActionsForControlEvents:UIControlEventTouchDragEnter];
        }

        if (self.currentButton && ![self.currentButton isEqual:draggedView]) {
            NSLog(@"Out: %ld", (long)self.currentButton.tag);

            // send exit event to your button
            [self.currentButton sendActionsForControlEvents:UIControlEventTouchDragExit];
            self.currentButton = nil;
        }
    } else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        self.currentButton = nil;
    }
}
like image 83
Aleksander Grzyb Avatar answered Sep 29 '22 14:09

Aleksander Grzyb