1

我有两个自定义 uibuttons。

@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 方法不是这样使用的?提前致谢!

4

1 回答 1

2

您的怀疑是正确的,这不是touchesCancelled:withEvent:打算使用的方式。从文档中:

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

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

似乎您想在触摸中更改与触摸事件关联的响应者,即当您从第一个按钮的边界拖动触摸并输入第二个按钮的边界时,它应该成为接收触摸事件的响应者。不幸的是,这不是响应者设计的工作方式。一旦 aUIView被识别为响应者,如中返回的那样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...
    }
}
于 2013-03-31T12:41:59.577 回答