4

我试图让UIButton我的一个视图上的一些实例被触摸并在屏幕上拖动(最终有动力,但那是以后的事了!)。我有一个非常简单的形式,如下所示,但问题是通过触摸按钮开始拖动它,它附着在手指上,并且通过抬起手指,触发“Touch Up Inside”事件,这是我在实际点击按钮时要执行的代码。

简而言之:我如何区分轻击和拖动/释放?我是否需要将点击更改为短点击手势识别器或类似的?代码:

在 viewDidLoad 中:

[firstButton addTarget: self action: @selector(wasDragged: withEvent:) forControlEvents: UIControlEventTouchDragInside];

而我的 wasDragged 方法:

- (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event
{
    if (button == letter1Button) {
        UITouch *touch = [[event touchesForView:button] anyObject];

        CGPoint previousLocation = [touch previousLocationInView:button];
        CGPoint location = [touch locationInView:button];
        CGFloat delta_x = location.x - previousLocation.x;
        CGFloat delta_y = location.y - previousLocation.y;

        button.center = CGPointMake(button.center.x + delta_x, button.center.y + delta_y);
    }
}
4

3 回答 3

16

您可以使用 aUIPanGestureRecognizer并告诉它取消视图中的触摸...

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIPanGestureRecognizer *panRecognizer;
    panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                            action:@selector(wasDragged:)];
    // cancel touches so that touchUpInside touches are ignored
    panRecognizer.cancelsTouchesInView = YES;
    [[self draggableButton] addGestureRecognizer:panRecognizer];

}

- (void)wasDragged:(UIPanGestureRecognizer *)recognizer {
    UIButton *button = (UIButton *)recognizer.view;
    CGPoint translation = [recognizer translationInView:button];

    button.center = CGPointMake(button.center.x + translation.x, button.center.y + translation.y);
    [recognizer setTranslation:CGPointZero inView:button];
}

- (IBAction)buttonWasTapped:(id)sender {
    NSLog(@"%s - button tapped",__FUNCTION__);
}
于 2012-11-22T12:22:29.380 回答
2

对于像我这样的初学者,我UIPanGestureRecognizer按照上面的建议进行了尝试,但没有奏效。所以,这是我的简单解决方案:首先,按照 Baig 的建议添加事件侦听器:

// add drag listener
[button addTarget:self action:@selector(wasDragged:withEvent:) forControlEvents:UIControlEventTouchDragInside];
// add tap listener
[button addTarget:self action:@selector(wasTapped:) forControlEvents:UIControlEventTouchUpInside];

拖动和点击都会触发UIControlEventTouchUpInside,因此添加一个标志,wasDragged:withEvent:如下所示:

-(IBAction)wasDragged: (id)sender withEvent: (UIEvent *) event {
    was_dragged = YES;
    UIButton *selected = (UIButton *)sender;
    selected.center = [[[event allTouches] anyObject] locationInView:self.view];
}

- (IBAction)buttonWasTapped:(id)sender {
    if(!was_dragged)
        NSLog(@"button tapped");
    else{
        was_dragged = NO;
        NSLog(@"button dragged");
    }
}

瞧。完毕。

于 2016-10-09T15:14:31.973 回答
1

使用 UIButton 的 UIControlEventTouchDragInside 和 UIControlEventTouchUpInside 事件,例如

// add drag listener
[button addTarget:self action:@selector(wasDragged:withEvent:) forControlEvents:UIControlEventTouchDragInside];
// add tap listener
[button addTarget:self action:@selector(wasTapped:) forControlEvents:UIControlEventTouchUpInside];

您可以使用HBDraggableButton控件..

于 2013-10-11T14:15:54.533 回答