我正在尝试在 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/