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

試著忘記壹切 提交于 2019-12-24 03:49:30

问题


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

回答1:


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



回答2:


Swift version - imagine you've connected your UIRotationGestureRecognizer to an IBAction and are passing in the UIRotationGestureRecognizer as argument recognizer. You could very easily test direction with the following:

if recognizer.rotation < 0 {
    print("Negative rotation value means anticlockwise rotation detected")
} else if recognizer.rotation > 0 {
    print("Positive rotation value means clockwise rotation detected")
}


来源:https://stackoverflow.com/questions/6390457/iphone-development-uirotationgesturerecognizer-clockwise-counter-clockwise

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!