IOS. Long press and drag to select another button. (Like the keyboard)

I'm having a hard time finding the right documentation for handling touch events to maintain similar behavior with the keyboard.

I want the button to show the custom view controller above the button when I long press it, but I want the user to be able to drag their finger to one of the other buttons (without lifting their finger off the screen).

I have a long press button and a custom view controller is configuring and working. I can't figure out how to support dragging from the first button to another button in the view controller to be able to select it.

I've tried using subclassing UIButton where I've tried this:

[self addTarget:self action:@selector(onDragOver:) forControlEvents:UIControlEventTouchDragEnter];

But it doesn't work.

I also found this question How to track button selection after long press? which is exactly the functionality I am trying to duplicate. But there are no answers.

+3


source to share


1 answer


Here is my solution. The trick is that you have to use hitTest :.

First, you add a gesture recognizer to the button, which is a regular button - the button that you want to open the context menu / custom view controller.



Then in your gesture recognizer callback you use hitTest: find out if the user is over the custom button and update it manually.

- (id) init {
    //add a long press gesture recognizer
    UILongPressureGestureRecognizer * gesture = [[UILongPressureGestureRecognizer alloc] initWithTarget:self action:@selector(onLongTap:)];
    [self.myButton addGestureRecognizer:gesture];
}

- (void) onLongTap:(UIGestureRecognizer *) gesture {

    if(gesture.state == UIGestureRecognizerStateBegan) {
        //display your view controller / context menu over the button
    }

    if(gesture.state == UIGestureRecognizerStateEnded) {
        //gesture stopped, use hitTest to find if their finger was over a context button

        CGPoint location = [gesture locationInView:self.view];
        CGPoint superviewLocation = [self.view.superview convertPoint:location fromView:self.view];
        UIView * view = [self.view.superview hitTest:superviewLocation withEvent:nil];

        if([view isKindOfClass:[MMContextMenuButton class]]) {
            //their finger was over my custom button, tell the button to send actions
            MMContextMenuButton * button = (MMContextMenuButton *) view;
            [self hideAndSendControlEvents:UIControlEventTouchUpInside];
            if(self.draggedContextMenuButton == button) {
                self.draggedContextMenuButton = nil;
            }
        }

        if(self.draggedContextMenuButton) {
            [self sendActionsForControlEvents:UIControlEventTouchUpInside];
        }

        self.draggedContextMenuButton = nil;
    }

    if(gesture.state == UIGestureRecognizerStateChanged) {
        //gesture changed, use hitTest to see if their finger
        //is over a button. Manually have to tell the button
        //that it should update it state.

        CGPoint location = [gesture locationInView:self.view];
        CGPoint superviewLocation = [self.view.superview convertPoint:location fromView:self.view];
        UIView * view = [self.view.superview hitTest:superviewLocation withEvent:nil];

        if([view isKindOfClass[MMContextMenuButton class]]) {
            MMContextMenuButton * button = (MMContextMenuButton *) view;
            if(self.draggedContextMenuButton != button) {
                [self.draggedContextMenuButton dragOut];
            }
            self.draggedContextMenuButton = button;
            [button dragOver];
        }
    }
}


//////////////


#import "MMContextMenuButton.h"
#import "MMContextMenus.h"

@implementation MMContextMenuButton

- (id) initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    self.layer.cornerRadius = 4;
    self.adjustsImageWhenHighlighted = FALSE;
    self.adjustsImageWhenDisabled = FALSE;
    self.backgroundColor = [UIColor clearColor];
    [self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [self setTitleColor:[UIColor colorWithRed:0.435  green:0.745  blue:0.867 alpha:1] forState:UIControlStateNormal];
    [self addTarget:self action:@selector(onHighlight:) forControlEvents:UIControlEventTouchDown];
    [self addTarget:self action:@selector(onRelease:) forControlEvents:UIControlEventTouchUpOutside&UIControlEventTouchUpOutside];
    return self;
}

- (void) onHighlight:(id) sender {
    self.backgroundColor = [UIColor colorWithRed:0.435  green:0.745  blue:0.867 alpha:1];
}

- (void) onRelease:(id) sender {
    self.backgroundColor = [UIColor clearColor];
}

- (void) hideAndSendControlEvents:(UIControlEvents) events {
    [self dragOut];
    [self sendActionsForControlEvents:events];
    [[MMContextMenus instance] hideContextMenus];
}

- (void) dragOver {
    self.highlighted = TRUE;
    self.backgroundColor = [UIColor colorWithRed:0.435  green:0.745  blue:0.867 alpha:1];
}

- (void) dragOut {
    self.highlighted = FALSE;
    self.backgroundColor = [UIColor clearColor];
}

@end

      

+1


source







All Articles