2010-11-24 2 views
0

Я обнаружил очень странную проблему при обработке событий касания. Идея приложения заключается в том, что у меня есть ImageView, который содержит круг с текстом, который пользователь может вращать. Я реализовал подклассу UIScrollView, чтобы содержать изображение окружности. Там я реализовал методы touchhesBegan, touchhesMoved и touchesEnded, чтобы повернуть мой круг, когда пользователь перетаскивает его влево или вправо. Все работает нормально, но когда вы пытаетесь перетащить его одним пальцем очень быстро с одной стороны на другую, а в противоположном направлении, методы касаютсяBegan и touchEnded, называются разным количеством раз. Например, прикосновенияBegan называли 1 раз и касались 2 - 3 раза. Как это может быть?проблема с событиями касания

- (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 2010-11-24 10:56:58

+0

Хм, я это сделаю, но дело в том, что я читаю количество точек касания: if ([[event touchsForView: self] count] == ​​1). Наверное, это как-то не работает – Andrew 2010-11-24 11:57:30

ответ

0

UIScrollView touch 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. 
Смежные вопросы