【问题标题】:How to rotate and resize uiview at same time?如何同时旋转和调整uiview?
【发布时间】:2013-05-28 03:40:09
【问题描述】:

我正在使用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)];

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

谢谢。

【问题讨论】:

    标签: iphone uiview resize


    【解决方案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];
              }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-25
      • 1970-01-01
      • 2011-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多