简体   繁体   中英

How to rotate and resize uiview at same time?

I'm using SPUserResizableView to resize my view. i had added rotation funcitionality to it . when i touch corner handel view should rotate for this purpose i have added some code but its not working properly. here is my code :

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

Note: when user touch corner handel a method get called in which i have written this piece of code. If user touches edges handel that method wont called.

Thanks.

try this :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM