2012-07-20 133 views
0

我的主視圖上有一堆UIView。我已將touchesBegan,touchesMoved和touchesEnded功能添加到應用程序中。iOS - touchesmoved每個視圖只有一次

移動時,該點處的視圖會多次顯示,除非觸摸超出視圖的CGRect。我想是指每個視圖,只有當移過視圖,而不是被認爲除非移出的CGRect,然後回到了曾經在這裏是我的代碼:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 

    UITouch *myTouch = [touches anyObject]; 

    CGPoint point = [myTouch locationInView:self.view]; 

    UIView *movedView = [self viewAtPoint:point]; 

    if(movedView != nil) 
    { 
    NSLog(@"Touches Moved - %@",movedView.name); 
    } 
} 


-(UIView *) viewAtPoint:(CGPoint) point 
{ 
CGRect touchRect = CGRectMake(point.x, point.y, 1.0, 1.0); 

    for(UIView *c in viewArray.views){ 
     if(CGRectIntersectsRect(c.frame, touchRect)){ 
      return c; 
     }  
    } 
    return nil; 
} 

所以NSLog轉儲了同樣的觀點多次。我只想在移動視圖時將其限制爲一次,直到再次移出視圖。

有什麼建議嗎?最後一個觀點

回答

1

跟蹤,觸摸遷入,然後您可以優化功能,以檢查觸摸是否仍然在該視圖中第一個對他人前檢查

+0

有時也可以是相同的觀點兩次但這一舉措必須超出視野範圍,然後重新計爲兩次。所以,如果我跟蹤最後一個視圖,我無法連續兩次獲取相同的視圖。 – peace4theapes 2012-07-20 19:19:20

+1

是的,你可以,當你移動時,你只需檢查它是否仍然在那個視圖中,如果它不是,那麼你將currentView重置爲零,然後當它再次移回到原始視圖時,它會註冊爲新的命中 – 2012-07-20 19:20:25

相關問題