触摸按钮,无需从屏幕上移开手指

时间:2012-11-12 12:33:20

标签: objective-c ios

有没有办法在没有用户从屏幕上移开手指的情况下检测UIButton内部的触摸?

实施例: 如果您有两个按钮,并且用户点击了左侧按钮,则将手指向右拖动,应用程序必须识别出您正在点击右侧按钮。

2 个答案:

答案 0 :(得分:2)

您应该可以使用现有的按钮事件来执行此操作。例如“触摸拖动外部”,“触摸外部”,“触摸拖动退出”等

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

答案 1 :(得分: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;
    }
}

无论如何都是这样的。