2010-11-24 8 views
0

터치 이벤트를 처리하는 동안 매우 이상한 문제가 발견되었습니다. 응용 프로그램의 아이디어는 텍스트가있는 원이 포함 된 ImageView가 있으며 사용자가 회전 할 수 있다는 것입니다. 원 이미지를 포함하도록 사용자 지정 UIScrollView 하위 클래스를 구현했습니다. 거기에 touchesBegan, touchesMoved 및 touchesEnded 메소드를 구현하여 사용자가 왼쪽이나 오른쪽으로 드래그 할 때 내 원을 회전시킵니다. 모든 것이 잘 작동하지만 한 손가락으로 다른 손가락과 다른 손가락으로 매우 빠르게 반대 방향으로 드래그하면 메서드 touchesBegan 및 touchesEnded가 다른 횟수로 호출됩니다. 예를 들어 touchesBegan은 1 번 호출되었고 touchesEnded는 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

가 시도? – marko

+0

흠, 나는 그것을 핥을 것이다. 그러나 그 점은 내가 터치 포인트의 수를 확인하는 것이다. if ([[이벤트 touchesForView : self] count] == ​​1). 아마 이것은 어떻게 든 작동하지 않습니다. – Andrew

+0

아니요, 다중 터치를 사용하지 않으면 문제가 해결되지 않습니다. ( – Andrew

답변

0

UIScrollView는 touch 이벤트를 매우 심하게 처리합니다. scrollView를 드래그하여 발생했을 수 있습니다.

는 multipleTouchEnabled이

- (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