0

SPUserResizableView用来调整我的视图。我为它添加了旋转功能。当我触摸角手柄视图应该为此目的旋转时,我添加了一些代码,但它不能正常工作。这是我的代码:

    CGPoint center = CGPointMake(CGRectGetMidX([view bounds]), CGRectGetMidY([view bounds]));
    CGPoint currentTouchPoint = [touch locationInView:view];
    CGPoint previousTouchPoint = [touch previousLocationInView:view];

    CGFloat angleInRadians = atan2f(currentTouchPoint.y - center.y, currentTouchPoint.x - center.x) - atan2f(previousTouchPoint.y - center.y, previousTouchPoint.x - center.x);

    [self setTransform:CGAffineTransformRotate([view transform], angleInRadians)];

注意:当用户触摸角落手柄时,会调用我编写这段代码的方法。如果用户触摸边缘手柄,则不会调用该方法。

谢谢。

4

1 回答 1

1

尝试这个 :

          UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
          [pinchRecognizer setDelegate:self];
          [imgBg addGestureRecognizer:pinchRecognizer];  // imgBg is my imageview you can set your view

          UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
          [rotationRecognizer setDelegate:self];
          [imgBg addGestureRecognizer:rotationRecognizer];



          #pragma mark -
          #pragma mark - GestureRecognizer Method

          -(void)scale:(id)sender {
              [viewGesture bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view]];
              if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
                  lastScale = 1.0; // declacre float instance in .h file
                  return;
              }
              CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
              CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
              CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
              [[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
              lastScale = [(UIPinchGestureRecognizer*)sender scale];
          }

          -(void)rotate:(id)sender {
              [viewGesture bringSubviewToFront:[(UIRotationGestureRecognizer*)sender view]];
              if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
                  lastRotation = 0.0;
                  return;
              }
              CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);
              CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
              CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
              [[(UIRotationGestureRecognizer*)sender view] setTransform:newTransform];
              lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
          }
于 2013-06-01T11:42:48.440 回答