【问题标题】:ios touch move from one button to another buttonios touch 从一个按钮移动到另一个按钮
【发布时间】:2013-03-26 15:28:23
【问题描述】:

我有两个自定义 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 方法不是这样使用的? 提前致谢!

【问题讨论】:

  • 我相信当您将手指移出第一个按钮区域时,touchesCanceled 已经被调用。它不会在第二个按钮上调用touchesBegan,因为您没有再次放下手指。为什么不使用手势识别器呢?
  • 您正在将事件从一个按钮传递到另一个按钮。一个按钮上的事件位置将与其他按钮不匹配,因此显然根据您的代码,甚至会在其他按钮中被取消。

标签: ios objective-c cocoa-touch uibutton


【解决方案1】:

您的怀疑是正确的,这不是 touchesCancelled:withEvent: 的用途。来自文档:

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

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

似乎您想在触摸中更改与触摸事件关联的响应者,即当您从第一个按钮的边界拖动触摸并进入第二个按钮的边界时,它应该成为接收触摸事件的响应者.不幸的是,这不是响应者设计的工作方式。一旦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...
    }
}

【讨论】:

    猜你喜欢
    • 2020-03-03
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多