2009-02-10 31 views
20

我有一个UIImage视图响应触摸事件。我想要取消触摸顺序,即,如果触摸超出特定范围,将进一步拨打touchesMoved:。我怎样才能做到这一点?如何取消一系列UITouch事件?

我知道在touchesMoved:我可以检查触摸对象的坐标并忽略它,但我不知道如何完全取消该序列。我没有看到Apple Developer UIResponder Reference中记录的任何方法可以调用以取消触摸顺序。

回答

6

这种解决方案可能有点缺憾,但你可以实现和手动调用

- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event; 

我在哪里我修改了对一些调整,我做了对苹果的iPhone sample code网站了MoveMe示例应用程序松散立足这一解决方案touchesMoved方法是这样的:

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    if ([touch view] == placardView) 
     CGPoint location = [touch locationInView:self]; 
     placardView.center = location; 
     // I added the following line: 
     [self touchesCancelled:touches withEvent:event]; 
     return; 
} 
+1

谢谢,当我回到我的Mac时,我会试试这个。但是这是否会取消随后的触动事件?在我看来,经过手动调用touchCancelled后,touchesMoved仍然会被系统调用。 – 2009-02-12 04:44:32

+0

在MoveMe示例代码的范围内,它将取消触摸,但我相信你是对的,touchesMoved仍然会被调用。我越想它,我认为触及得越少就是正确的召唤。 – 2009-02-12 21:55:51

+0

这不起作用,“移动”事件不断发生。 – Andy 2018-01-31 14:04:27

2

我最近遇到了同样的问题,并发现了一个标准的方式来解决这个问题。 您可以使用[[UIApplication sharedApplication] beginIgnoringInteractionEvents]停止将touchesMoved事件传递到整个应用程序。当您需要再次接触触摸时,请确保使用[[UIApplication sharedApplication] endIgnoringInteractionEvents]启用它们。

3

你需要调用[super touchesMoved:withEvent:]为了让超级查看从事件,但更重要的是清理,你需要通话[super touchesCancelled:withEvent:]

这是我在小区中使用,以保持它得到选择时,我发现刷卡:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    if (!showingEdit) { 
     if (IS_FAR_ENOUGH_TO_BE_A_SWIPE) { 
      RESPOND_TO_SWIPE 
      showingEdit = YES; 
      [super touchesCancelled:touches withEvent:event]; 
     } else { 
      [super touchesMoved:touches withEvent:event]; 
     } 
    } 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    if (!showingEdit) { 
     [super touchesEnded:touches withEvent:event]; 
    } 
} 
+1

这不适合我。 – ma11hew28 2011-11-17 19:17:40

2

我不认为这是可能的,因为我没有看到它记录和这些解决方案工作。

-2

您可以检查,触摸点位置在的CGRect(即点都在矩形的ImageView的),或者没有。如果他们不在该Rect中,触摸将被取消。

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

    if (CGRectContainsPoint(myImageView, touchLocation)) 
    { 
     lastPoint = [touch locationInView: myImageView]; 
    } 

    NSLog(@"Last :%.2f - %.2f",lastPoint.x, lastPoint.y); 
    } 

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

    if (CGRectContainsPoint(myImageView.frame, touchLocation)) 
    { 
     currentPoint = [touch locationInView: myImageView]; 
    } 
} 
+1

该代码不会取消对“touchesMoved:”的进一步调用。它只是忽略它们。 – ma11hew28 2011-11-25 16:27:14

0

在iOS5中似乎有一个私有方法的UITouch

-(void)setSentTouchesEnded:(BOOL)ended; 

根据苹果的实现也可能停止发送事件

这样做的另一种方法是使用关联对象

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    if ([(NSNumber *)objc_getAssociatedObject(touch, &outKey) boolValue]) 
     return; 
    if (sometouchisoutsideofview) { 
     objc_setAssociatedObject(touch, &outKey, [NSNumber numberWithBool:YES], OBJC_ASSOCIATION_RETAIN); 
    } 
} 
3

尝试临时设置UIImageView的userInteraction启用属性为NO

0

我只是想解决这样的问题,并发现没有列出的解决方案她为我工作。我能做的最好的事情是暂时忽略了触摸,但是当触摸重新进入视图时,他们又恢复了。

终于解决了。

  1. 设置一个被称为“无效”
  2. 在touchedMoved,首先检查该触摸是所需视图内部布尔标志。如果不是,请将禁用标志设置为YES。
  3. 仍然在touchMoved,执行您的操作前检查国旗。 因此,在这一点上,如果他们不在视野范围内,将不会发生任何事情。
  4. 在touchesEnded中,将禁用标志设置为NO。因此,触摸顺序会有效停止,直到用户抬起手指。

我想这会为你工作,除非你有一个特定的原因需要按字面顺序取消。

1

“蛮力”解决方案取决于它是否适合您的需求:删除并重新初始化接收触摸或响应它的子视图(当然要照顾框架和内容)。

-1

您可以为UITouch创建类别。你可以申报

@property (nonatomic,strong,readwrite) UIView *view; 

当您更改touch.view为零,例如,您可以模拟调度触摸事件

1

此代码将帮助你结束。我在touchesMoved:方法禁用触摸事件,我通过从它的父视图并将其添加直背使触摸事件touchesCancelled:方法

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    ....... 
    ....... 
    Your code 
    ....... 
    ....... 

    //Condition when you want to disable touch event 

    [[UIApplication sharedApplication] beginIgnoringInteractionEvents]; 

    ....... 
    ....... 
    Your code 
    ....... 
    ....... 
} 


- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [[UIApplication sharedApplication] endIgnoringInteractionEvents]; 
} 
1

我实现这一目标。

[view retain]; 
UIView *sv = view.superview; 
[view removeFromSuperview]; 
[sv addSubview:view]; 
[view release]; 

这将响应者链分解为视图,以便在视图上不会接收任何剩余的触摸。下一次触摸仍然会正常接收。