iphone - 检测 UIButton 事件何时完成

标签 iphone objective-c

我正在尝试在 UIButton 上实现拖放功能,它工作正常,但我想不出一种方法来确定用户何时松开按钮并完成拖动。下面的代码适用于拖动,但我需要在用户完成拖动并松开按钮时得到通知。

- (void)viewDidLoad
{
    [gesturesBrowserButton addTarget:self action:@selector(wasDragged:withEvent:) 
     forControlEvents:UIControlEventTouchDragInside];

    [gesturesBrowserButton addTarget:self action:@selector(finishedDragging:withEvent:) 
                    forControlEvents:UIControlEventTouchDragExit];
}

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

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

    // move button
    button.center = CGPointMake(button.center.x + delta_x,
                                button.center.y + delta_y);

    NSLog(@"was dragged");
}

- (void)finishedDragging:(UIButton *)button withEvent:(UIEvent *)event
{
    //doesn't get called
    NSLog(@"finished dragging");
}

最佳答案

我认为替换

UIControlEventTouchDragExit

UIControlEventTouchDragExit|UIControlEventTouchUpInside

将是解决您的问题的良好开端。

关于iphone - 检测 UIButton 事件何时完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9067924/

相关文章:

ios - 网址无法从json解析中获得任何响应

iphone - iOS 多 View 问题

iphone - 如何编写在框中绘制文本/字符串的测试用例?

ios - TRON 网址问题 swift

objective-c - cocoa 获取已安装应用程序列表

ios - resultLabel 的本地化

iphone - 更改 UITextView 中所选单词/行的字体格式

iphone - 哪个适用于 iOS 的图像缓存库?

ios - 如何在 IOS 中扫描信标?

iPhone iOS5 CLGeocoder 如何对一大组(200)地址进行地理编码?