2013-03-26 131 views
1

我有两个自定义的uibuttons。ios触摸从一个按钮移动到另一个按钮

@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方法不是用于这样的? 在此先感谢!

+0

我相信'当你移动你的手指从第一个按钮的面积touchesCanceled'已经被调用。它不会在第二个按钮上调用touchesBegan,因为您没有再次放下手指。为什么不使用手势识别器呢? – 2013-03-26 16:01:42

+0

您正在将事件从一个按钮传递到另一个按钮。一个按钮上的事件位置与其他按钮不匹配,因此显然与您的代码一样,甚至会在其他按钮中取消。 – nkongara 2013-03-26 17:15:02

回答

2

您的暂停是正确的,但这不是打算如何使用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... 
    } 
} 
相关问题