2010-11-24 31 views
0

タッチイベントの処理中に非常に奇妙な問題が見つかりました。アプリのアイデアは、ユーザーが回転できるテキスト付きの円を含むImageViewがあることです。 サークルイメージを含むカスタムUIScrollViewサブクラスを実装しました。そこで私はtouchesBegan、touchesMoved、touchesEndedというメソッドを実装して、ユーザーが左右にドラッグするとサークルを回転させました。すべてがうまくいきますが、片方の指で非常に速く一方の側から他方の方向にドラッグしようとすると、touchesBeganとtouchesEndedのメソッドは異なる回数呼び出されます。たとえばtouchesBeganは1回呼び出され、2回〜3回touchesEndedされました。どのようにすることができますか?タッチイベントの問題

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    if([[event touchesForView:self] count]==1){ 

      touchPoint = [[touches anyObject] locationInView:self]; 
      previousTouchPoint = touchPoint; 
      angle = 0; 
      if (((touchPoint.x > 160)&&(touchPoint.y < 210))||((touchPoint.x < 160)&&(touchPoint.y > 210))) { 
       leftRotation = YES; 
      } 
      else { 
       leftRotation = NO; 
      } 

      currentMoveAngle = 0; 
     } 
    } 

    - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event{ 
     if([[event touchesForView:self] count] == 1){ 
       CGPoint newPoint = [[touches anyObject] locationInView:self]; 
       CGPoint origin; 
       if (self.tag == 2) { 
        origin = CGPointMake(self.bounds.origin.x+self.bounds.size.width*0.5, 215); 
       } 
       else { 
        origin = CGPointMake(self.bounds.origin.x+self.bounds.size.width*0.5, 215); 
       } 
       previousTouchPoint.x -= origin.x; 
       previousTouchPoint.y -= origin.y; 
       CGPoint second = newPoint; 
       second.x -= origin.x; 
       second.y -= origin.y; 
       CGFloat rotationAngle = [self rotationFromFirstPoint:previousTouchPoint toSecondPoint:second]; 

       previousTouchPoint = newPoint; 
       [self rotateContentToAngle:rotationAngle animated:NO]; 
       currentMoveAngle += rotationAngle; 
      } 

    } 

- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event{ 
    if ([[event touchesForView:self] count] == 1){ 
     rotating = YES; 
      CGFloat rotationAngle; 
      CGPoint origin = CGPointMake(self.bounds.origin.x+self.bounds.size.width*0.5, 215); 
      CGPoint lastPoint = [[touches anyObject] locationInView:self]; 
      CGPoint touchP = touchPoint; 
      if ((touchP.x != lastPoint.x)||(touchP.y != lastPoint.y)) { 
       touchP.x -= origin.x; 
       touchP.y -= origin.y; 
       lastPoint.x -= origin.x; 
       lastPoint.y -= origin.y; 

       if (fabs(currentMoveAngle)>M_PI/6) { 
        NSInteger index = (int)trunc(currentMoveAngle/(M_PI/3)); 
        currentMoveAngle-=(M_PI/3)*index; 
        NSLog(@"rotation index: %i",index); 
       } 

       if (leftRotation) { 
        rotationAngle = M_PI/3; 
        rotationAngle-=currentMoveAngle; 
       } 
       else { 
        rotationAngle = (-1)*M_PI/3; 
        rotationAngle-=currentMoveAngle; 
       } 

       [self rotateContentToAngle:rotationAngle animated:YES]; 
      } 
     } 
} 
+0

を試すには、multipleTouchEnabledですか? – marko

+0

hm、私はそれをチックするだろうが、事は私がタッチポイントの数を尋ねているということです:if([[event touchesForView:self] count] == 1)。おそらくこれはどういうわけか実際には機能しません – Andrew

+0

いいえ、複数のタッチを無効にしても問題は解決しません( – Andrew

答えて

0

UIScrollViewがtouchイベントを処理するのは非常に悪い.... scrollViewのドラッグによって起こっている可能性があります。

この

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    if (!self.dragging){ 
    if([[event touchesForView:self] count]==1){ 

     touchPoint = [[touches anyObject] locationInView:self]; 
     previousTouchPoint = touchPoint; 
     angle = 0; 
     if (((touchPoint.x > 160)&&(touchPoint.y < 210))||((touchPoint.x < 160)&&(touchPoint.y > 210))) { 
      leftRotation = YES; 
     } 
     else { 
      leftRotation = NO; 
     } 

     currentMoveAngle = 0; 
    } 
} 
} 

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event{ 
    if (!self.dragging){  
    if([[event touchesForView:self] count] == 1){ 
      CGPoint newPoint = [[touches anyObject] locationInView:self]; 
      CGPoint origin; 
      if (self.tag == 2) { 
       origin = CGPointMake(self.bounds.origin.x+self.bounds.size.width*0.5, 215); 
      } 
      else { 
       origin = CGPointMake(self.bounds.origin.x+self.bounds.size.width*0.5, 215); 
      } 
      previousTouchPoint.x -= origin.x; 
      previousTouchPoint.y -= origin.y; 
      CGPoint second = newPoint; 
      second.x -= origin.x; 
      second.y -= origin.y; 
      CGFloat rotationAngle = [self rotationFromFirstPoint:previousTouchPoint toSecondPoint:second]; 

      previousTouchPoint = newPoint; 
      [self rotateContentToAngle:rotationAngle animated:NO]; 
      currentMoveAngle += rotationAngle; 
     } 

} 
} 

- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event{ 
    if (!self.dragging){  
    if ([[event touchesForView:self] count] == 1){ 
    rotating = YES; 
     CGFloat rotationAngle; 
     CGPoint origin = CGPointMake(self.bounds.origin.x+self.bounds.size.width*0.5, 215); 
     CGPoint lastPoint = [[touches anyObject] locationInView:self]; 
     CGPoint touchP = touchPoint; 
     if ((touchP.x != lastPoint.x)||(touchP.y != lastPoint.y)) { 
      touchP.x -= origin.x; 
      touchP.y -= origin.y; 
      lastPoint.x -= origin.x; 
      lastPoint.y -= origin.y; 

      if (fabs(currentMoveAngle)>M_PI/6) { 
       NSInteger index = (int)trunc(currentMoveAngle/(M_PI/3)); 
       currentMoveAngle-=(M_PI/3)*index; 
       NSLog(@"rotation index: %i",index); 
      } 

      if (leftRotation) { 
       rotationAngle = M_PI/3; 
       rotationAngle-=currentMoveAngle; 
      } 
      else { 
       rotationAngle = (-1)*M_PI/3; 
       rotationAngle-=currentMoveAngle; 
      } 

      [self rotateContentToAngle:rotationAngle animated:YES]; 
     } 
     }  } 
     } 


or make scrollingEnabled property NO. 
+0

よく、プロパティscrollingEnabledはNOに設定されていましたが、私は既に試みました!(!self.dragging)効果なし...とにかく、答えをありがとう。 – Andrew

関連する問題