2011-10-27 72 views

回答

12

你可以用手勢識別去做。對於檢測單抽頭位置使用UITapGestureRecognizer

UITapGestureRecognizer *tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)] autorelease]; 
[myScrollView addGestureRecognizer:tapRecognizer]; 

- (void)tapAction:(UITapGestureRecognizer*)sender{ 
    CGPoint tapPoint = [sender locationInView:myScrollView]; 
    CGPoint tapPointInView = [myScrollView convertPoint:tapPoint toView:self.view]; 
} 

要轉換tapPoint到self.view您可以在UIView類使用convertPoint:toView:方法

+0

nope。如果我點擊並不滾動tapAction,就會觸發它。如果我喜歡我說的話,點擊,滾動,舉起手指,點擊,滾動,舉起手指等,就不會檢測到水龍頭。如果滾動條在大滑動之後也滾動,則無法檢測到水龍頭。滾輪必須靜止不動,而且必須製作一個乾式水龍頭才能使用。 – SpaceDog

+1

還有很多其他手勢識別器 - UIPanGestureRecognizer,UIPinchGestureRecognizer,UISwipeGestureRecognizer,UIRotationGestureRecognizer。此外,您可以創建自定義識別器來檢測非常複雜的觸摸。我用簡單的手勢給了更受歡迎的例子。 – beryllium

+0

@RubberDuck你有沒有想過這個?我在同一條船上......我正在研究UICollectionView,我想要檢測手指是否在單元格視圖上 –

0

你可以找到在視圖中的位置和滾動OFSET添加到它。現在,您的下一個問題是-(void)touchesBegan:touches:event將不會被調用,因爲事件將發送到您的滾動視圖。這可以通過子類化你的UIScrollView來解決,並讓scrollview把觸摸事件發送給下一個響應者(你的視圖)。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // Position of touch in view 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint touchPoint = [touch locationInView:self.view]; 

    // Scroll view offset 
    CGPoint offset = scrollView.contentOffset; 

    // Result 
    CGPoint scrollViewPoint = CGPointMake(touchPoint.x, touchPoint.y + offset.y); 
    NSLog(@"Touch position in scroll view: %f %f", scrollViewPoint.x, scrollViewPoint.y); 
} 
+0

nope。如果我點擊並不滾動,touchesBegan就會觸發。如果我喜歡我說的話,點擊,滾動,舉起手指,點擊,滾動,舉起手指等,就不會檢測到水龍頭。如果滾動條在大滑動之後也滾動,則無法檢測到水龍頭。滾輪必須靜止不動,而且必須製作一個乾式水龍頭才能使用。這看起來並不容易。 – SpaceDog