2012-04-01 43 views
9

所以我有一个UIView的子类,假设检测触摸。仅当触摸在当前视图内开始时,视图检测才会触摸。当触摸开始于视图之外并且它们在我的自定义视图中移动时touchesMoved不会被调用。检测当前视图中尚未启动的移动触摸的任何解决方案?iOS - 检测UIView中的触摸?

@implementation MycustomView 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // This only gets called if touches have started in the current View 
} 

@end 
+1

这是记录和预期的行为。也许如果你对一些人有所了解*你想完成什么,可以帮助你完成* how *。 – NJones 2012-04-01 23:14:50

+0

我在屏幕上有多个自定义视图我想检测UIViews作为触摸移动它们 – aryaxt 2012-04-01 23:16:18

回答

20

以下解决方案工作。我有多个MyCustomView实例;作为触摸移动我想检测正在接触

我结束了从MyCustomView移动触摸检测到它的父的意见,所以下面的代码不再MyCustomView类:

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

    for (UIView *view in self.contentView.subviews) 
    { 
     if ([view isKindOfClass:[MyCustomView class]] && 
      CGRectContainsPoint(view.frame, touchLocation)) 
     { 

     } 
    } 
} 
1

这应该修复它:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    for (UIView* subView in self.subviews) 
    { 
     if([subView pointInside:[self convertPoint:touch toView:subView] withEvent:event]) 
     { 
      //do your code here 
     } 
    } 
} 
+0

它的行为与我已有的相同。我的触摸传递5个不同的UIViews,并且它总是返回第一个视图,触摸从 – aryaxt 2012-04-01 23:10:02

+0

复制touchesMoved方法..看看你在做什么.. – skytz 2012-04-01 23:18:37

+0

代码正是你发布的,除了我有一个NSlog在它:NSLog(@“%@”,[touch view]); – aryaxt 2012-04-01 23:25:20

0

的一种方式要做到这一点(尽管可能有其他方法)是禁用子视图的用户交互,并使其父视图跟踪移动(使用hitTest方法来确定触摸当前处于哪个视图)。

0

试试这个....

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    for(UITouch *touch in touches) 
    { 
     CGPoint touchPointFirstBtn = [touch locationInView:self.ChordView]; 
     if(CGRectContainsPoint(_btnC.frame, touchPointFirstBtn)) 
     { 
      if (!_btnC.isHighlighted) 
      { 
       if(!Boolean) 
       { 
        title = @"C"; 
        [_tlbView reloadData]; 
        NSLog(@"%@",@"touches C"); 

       } 
       [_btnC setHighlighted:YES]; 
       Boolean = YES; 

      } 
     } 
     else 
     { 
      [_btnC setHighlighted:NO]; 
      Boolean = NO; 
     } 
}