iOS:处理长按并拖动以选择另一个按钮。 (像键盘一样)

时间:2015-07-23 05:29:20

标签: ios cocoa-touch keyboard touch

我很难找到如何处理触摸事件的正确文档,以支持键盘的类似行为。

我想要的是一个按钮,当我长按它时,它会在按钮上方显示一个自定义视图控制器,但我希望用户能够将手指拖动到其他按钮之一(不要将手指拉开)屏幕)。

我有一个长按按钮,它的自定义视图控制器都设置和工作。我无法想象的是如何支持从第一个按钮拖动到视图控制器中的另一个按钮,以便能够选择它。

我尝试过使用子类UIButton,我试过这个:

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

但这不起作用。

我还发现了这个问题How to track button selection after long press?,这正是我试图复制的功能。但是没有答案。

1 个答案:

答案 0 :(得分:2)

这是我的解决方案。诀窍是你必须使用hitTest:。

首先,您将手势识别器添加到作为普通按钮的按钮 - 您要打开上下文菜单/自定义视图控制器的按钮。

然后在你的手势识别器回调中,你使用hitTest:来判断用户是否超过你的自定义按钮并手动更新它的状态。

- (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's 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