iOS:长按并拖动以选择另一个按钮.(就像键盘一样) [英] iOS: Handling long press and drag to select another button. (Like the keyboard)

查看:70
本文介绍了iOS:长按并拖动以选择另一个按钮.(就像键盘一样)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难找到有关如何处理触摸事件的正确文档,以支持与键盘相似的行为.

I'm having a hard time finding the right documentation for how to handle touch events in order to support similar behavior to the keyboard.

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

What I want is a button that when I long press it, it shows a custom view controller above the button, but I want the user to be able to drag their finger to one of the other buttons (without taking their finger off the screen).

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

I have the button with a long press and it's custom view controller all setup and working. What I can't figure is how to support dragging from the first button over to the other button in the view controller to be able to select it.

我尝试使用子类化的UIButton进行尝试:

I've tried using a subclassed UIButton where I tried this:

<代码>[self addTarget:self动作:@selector(onDragOver :) forControlEvents:UIControlEventTouchDragEnter];

但这不起作用.

我也发现了这个问题>如何在长按后跟踪按钮的选择?正是我要复制的功能.但是没有答案.

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

推荐答案

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

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

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

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

然后在手势识别器回调中,使用hitTest:判断用户是否位于您的自定义按钮上方,并手动更新其状态.

Then in your gesture recognizer callback, you use hitTest: to figure out if the user is over a custom button of yours and update it's state 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'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

这篇关于iOS:长按并拖动以选择另一个按钮.(就像键盘一样)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆