2013-06-01 69 views
0

我正在使用SPUserResizableView來調整我的視圖。我已經添加了旋轉功能。當我碰角落漢德爾視圖應該旋轉爲此目的我已經添加了一些代碼,但它不能正常工作。這裏是我的代碼:如何同時旋轉和調整uview的大小?

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

試試這個:

  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]; 
      } 
相關問題