TouchesCancelled Problem in Gesture recognition

1 Comment


While I was working with touch events and gesture recognition , I ran into a peculiar issue. While I swipe down the screen, touches began and touches moved got called and also touches ended at the last. But many times touches cancelled event was called while trying to swipe. In some cases, I was trying to rotate the image slowly, and touches cancelled event got called and it stopped the occurrence of touches moved events. It ended the gesture event in middle and I have to start from the first, i.e from touches began event. We were thinking that touches got cancelled because of the memory warning, but we had no memory warning in our application at all.I had no clue why touches cancelled event got called.

My objective was to rotate an image based on touch events. I was experimenting some code snippets with two different ways. One method is by using UIResponder chain and getting UITouch events(began ,moved and ended). Another method is by using UIGestureRecognizer. I implemented both methods in single application and was developing an application to rotate image. After few experiments, I stick with the first method. I removed only the implementation methods of UIGestureRecognizer.

After struggling a lot, surfing through google, I got an answer. It was due to UIGestureRecognizer. If a gesture recognizer recognizes its gesture, it unbinds the remaining touches of that gesture from their view.The window cancels the previously delivered touches with a touches Cancelled message. After removing all the UIGestureRecognizer codes, now it works fine. I also read that we can also turn off the touches cancel in view property to avoid this behavior, which is yet to be tested.

Links:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html#//apple_ref/occ/cl/UIGestureRecognizer

Advertisements

Direction between two touch Points

Leave a comment


The below code provides the direction of the swipe between two points:



+(int) getDirectionBetweenFirst:(CGPoint)point1 secondPoint:(CGPoint)point2 {
    
    DIRECTION_TYPE directionType = CLOCKWISE;
    
    if (point1.x < point2.x  ) {
        
        if(point1.y > point2.y){
            
            directionType = CLOCKWISE;
            
        }else if(point1.y < point2.y)
        {
            directionType = COUNTERCLOCKWISE;
            
        }else{
            
            if(point1.y <= kCenterYCoordinate){
                directionType = CLOCKWISE;
            }else{
                directionType = COUNTERCLOCKWISE;
                
            }
      
        }
    }
    else if(point1.x > point2.x  ){
        
        if(point1.y > point2.y){
             directionType = COUNTERCLOCKWISE;
        }else if(point1.y < point2.y)
        {
            directionType = CLOCKWISE;
            
        }
        else{
            
            if(point1.y <= kCenterYCoordinate){
                directionType = COUNTERCLOCKWISE;
            }else{
                directionType = CLOCKWISE;
                
            }
        }
    }else{
        
        if (point1.y < point2.y) {
            
            if(point1.x <= kCenterXCoordinate){
                directionType = COUNTERCLOCKWISE;
            }else{
                directionType = CLOCKWISE;
                
            }
        }else if(point1.y > point2.y){
            
            if(point1.x <= kCenterXCoordinate){
                directionType = CLOCKWISE;
            }else{
                directionType = COUNTERCLOCKWISE;
                
            }
        }
        
    }
   
    return directionType;
}


kCenterYCoordinate and kCenterXCoordinate are center point of the UIView on which you detect your touches.