旋转时将uiview捕捉到特定角度,锁定它并且不能再移动

时间:2013-05-19 00:34:41

标签: ios uiview rotation uigesturerecognizer cgaffinetransform

我只想使用能够旋转到0度,90度,180度,270度,而不是介于两者之间。但即使在旋转之后,如果需要,我希望用户不要被锁定并再次旋转。在我让视图自由旋转之前,现在我正在改变我的代码。

myView是我正在旋转的视图

 - (IBAction)rotation:(UIRotationGestureRecognizer *)recognizer {

     UIView *myView = recognizer.view;
     switch (recognizer.state) {
        case UIGestureRecognizerStatePossible:
           break;
        case UIGestureRecognizerStateBegan:
           break;
        case UIGestureRecognizerStateChanged: {
           CGFloat rotation = [recognizer rotation];
           CGAffineTransform rotate = CGAffineTransformMakeRotation(rotation);
           CGAffineTransform transform = CGAffineTransformConcat(myView.transform, rotate);
           myView.transform = transform;
           [recognizer setRotation:0.0];          
        }
           break;
        case UIGestureRecognizerStateEnded:
        {
           NSLog(@"state ended");
           CGFloat radians = atan2f(myView.transform.b, myView.transform.a);
           CGFloat degrees = radians * (180 / M_PI);
           NSLog(@"%f",degrees);

           if (degrees < 90.0) {
             NSLog(@"less than 90 degree");

             myView.transform = CGAffineTransformMakeRotation(radians);
             [recognizer setRotation:1.570796330];
           }
         }
         break;
       default:
         break;
}
然后我尝试了:

    case UIGestureRecognizerStateEnded:
    {
        NSLog(@"state ended");
        CGFloat radians = atan2f(myView.transform.b, myView.transform.a);
        CGFloat degrees = radians * (180 / M_PI);
        NSLog(@"%f",degrees);

        if (degrees < 90.0) {
            NSLog(@"less than 90 degree");

            myView.transform = CGAffineTransformMakeRotation(M_PI_2);
        }
    }
         break;
     default:
         break; 
}

然后将其调到90度,然后将其锁定在那里,以便我无法再旋转uiview。

有什么想法吗? 感谢。

1 个答案:

答案 0 :(得分:1)

问题是您在break案例结束时忘记了UIGestureRecognizerStateChanged