开发者

touch events problem

I found a very strange problem while handling touch events. The idea of an app is that i have an ImageView which contains a circle with text, that the user can rotate. I've implemented a开发者_高级运维 custom UIScrollView subclass to contain circle image. There i implemented methods touchesBegan, touchesMoved and touchesEnded in order to rotate my circle when user drags it left or right. Everything works fine, but when you try to drag it with one finger very fast from one side to another and in opposite direction, methods touchesBegan and touchesEnded are called different number of times. For example touchesBegan was called 1 time and touchesEnded 2 - 3 times. How can it be?

   - (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];
            }   
        }
}


UIScrollView handle touch event very badly.... may be it is happening because of dragging of scrollView.

try this

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜