如何同时旋转和调整uiview的大小?

时间:2013-06-01 11:22:58

标签: iphone uiview resize

我正在使用SPUserResizableView调整视图大小。我已经为它添加了旋转功能。当我触摸角handel视图应旋转为此目的我添加了一些代码,但它不能正常工作。这是我的代码:

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

注意:当用户触摸角handel时,会调用一个方法来编写这段代码。如果用户接触到该方法不会调用的边缘,则

感谢。

1 个答案:

答案 0 :(得分: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];
          }