ios touch从一个按钮移动到另一个按钮

时间:2013-03-26 15:28:23

标签: ios objective-c cocoa-touch uibutton

我有两个自定义的uibutton。

@interface myButton : UIButton

我覆盖了几个方法,比如

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  // do something
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];
    [self.nextResponder touchesMoved:touches withEvent:event];
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];

    if (!CGRectContainsPoint(self.bounds, touchPoint)) {
        [self touchesCancelled:touches withEvent:event];
    }
    return;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
   // do something
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesCancelled:touches withEvent:event];
    return;
}

我想要的是,当我触摸按钮时,我可以继续将触摸移动到另一个按钮。当触摸超出我触摸的第一个按钮的界限时,我厌倦了调用touchesCancelled。所以我“想”然后我移动到另一个按钮它将是一个新的触摸事件。但它不会像这样工作。

我做错了什么吗?或者touchesCancelled方法不是这样使用的? 提前谢谢!

1 个答案:

答案 0 :(得分:2)

您的疑问是正确的,这不是touchesCancelled:withEvent:的意图。来自文档:

  

当Cocoa Touch框架收到需要取消触摸事件的系统中断时,将调用此方法;为此,它生成一个UITouch对象,其相位为UITouchPhaseCancel。中断可能导致应用程序不再处于活动状态,或者视图将从窗口中删除。

如果您的用户收到来电,短信或他们的闹钟响起等,您的响应人员将收到触摸取消事件。它应该用于清除在其他触摸事件中建立的任何状态信息。

好像你想要改变与触摸中间触摸事件相关联的响应者,即当您从第一个按钮的边界拖动触摸并输入第二个按钮的触摸时,它应该成为接收触摸事件的响应者。不幸的是,这不是响应者的工作方式。一旦UIView被识别为响应者,如hitTest:withEvent:中所返回,这将是UIView接收触摸事件。

在包含两个按钮的超级视图中,实现您想要的可能的锻炼方法是处理触摸事件(touchesBegan:withEvent:touchesMoved:withEvent:等)。然后您的超级视图将接收触摸事件,您可以根据您所在的按钮框架采取措施:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch *touch in touches)
    {
        CGPoint touchLocation = [touch locationInView:self.view];

        UIButton *button;
        if (CGRectContainsPoint(self.button1.frame, touchLocation))
        {
            button = self.button1;
            NSLog(@"touchesMoved: First Button");
        }
        else if (CGRectContainsPoint(self.button2.frame, touchLocation))
        {
            button = self.button2;
            NSLog(@"touchesMoved: Second Button");
        }

        // Do something with button...
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch *touch in touches)
    {
        CGPoint touchLocation = [touch locationInView:self.view];

        UIButton *button;
        if (CGRectContainsPoint(self.button1.frame, touchLocation))
        {
            button = self.button1;
            NSLog(@"touchesMoved: First Button");
        }
        else if (CGRectContainsPoint(self.button2.frame, touchLocation))
        {
            button = self.button2;
            NSLog(@"touchesMoved: Second Button");
        }

        // Do something with button...
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch *touch in touches)
    {
        CGPoint touchLocation = [touch locationInView:self.view];

        UIButton *button;
        if (CGRectContainsPoint(self.button1.frame, touchLocation))
        {
            button = self.button1;
            NSLog(@"touchesEnded: First Button");
        }
        else if (CGRectContainsPoint(self.button2.frame, touchLocation))
        {
            button = self.button2;
            NSLog(@"touchesEnded: Second Button");
        }

        // Do something with button...
    }
}