2012-04-01 44 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; 
     } 
}