Search code examples
iphonegesture

iphone development - UIRotationGestureRecognizer (Clockwise & counter Clockwise detection)?


Is there a way to detect if the rotation done by the user is in clockwise or counter clockwise direct???

I searched for that but couldn't find an answer !

My code looks like this:

 UIGestureRecognizer *recognizer;
 recognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(spin)];
 [self.rotating addGestureRecognizer:recognizer];
 [recognizer release];

Solution

  • the rotation property will tell you the rotation in radians. A negative value would indicate the rotation is clockwise, and a positive value indicates counter-clockwise.

    Example:

    UIGestureRecognizer *recognizer;
    recognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(spin:)];  // <-- Note the colon after the method name
    [self.rotating addGestureRecognizer:recognizer];
    [recognizer release];
    
    - (void)spin:(UIGestureRecognizer *)gestureRecognizer {
        CGFloat rotation = gestureRecognizer.rotation;
        if (rotation < 0) {
            // clockwise
        } else {
            // counter-clockwise
        }
    }