0

有没有一种方法可以检测 UIButton 内部的触摸,而无需用户从屏幕上移开手指?

示例:如果您有两个按钮,并且用户点击了左侧的一个,然后将手指拖到右侧,应用程序必须识别出您正在点击右侧的按钮。

4

2 回答 2

2

您应该能够使用已经存在的按钮事件来执行此操作。例如“Touch Drag Outside”、“Touch Up Outside”、“Touch Drag Exit”等。

只需注册这些活动,看看哪些活动适合您的需求。

于 2012-11-12T12:57:05.143 回答
0

我会使用 UIViewController 自己实现。

而不是使用按钮。

在屏幕上放置两个视图(每个按钮一个)您可以制作这些按钮、imageViews 或只是 UIViews,但要确保它们具有userInteractionEnabled = NO;.

然后在 UIViewController 中使用方法touchesBegantouchesMoved.

我会在 viewController 中保存一些状态,比如......

BOOL trackTouch;
UIView *currentView;

然后,如果 touchesBegan 在您的某个视图中...

-(void)touchesBegan... (can't remember the full name)
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    if (CGRectContainsPoint(firstView, point)) {
        trackTouch = YES
        //deal with the initial touch...
        currentView = firstView;  (work out which view you are in and store it)
    } else if (CGRectContainsPoint(secondView, point)) {
        trackTouch = YES
        //deal with the initial touch...
        currentView = secondView;  (work out which view you are in and store it)
    }
}

然后在touchesMoved...

- (void)touchesMoved... (can't remember the full name)
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    if (CGRectContainsPoint(secondView, point) and currentView != secondView)) {
        // deal with the touch swapping into a new view.
        currentView = secondView;
    } else if (CGRectContainsPoint(firstView, point) and currentView != firstView)) {
        // deal with the touch swapping into a new view.
        currentView = firstView;
    }
}

反正是这样的。

于 2012-11-12T12:43:14.470 回答