Alexyuiop
Alexyuiop

Reputation: 823

Rotating UIView Cause It to Change Position

I'm trying to rotate a uiview around its center on drag (UIPanGestureRecognizer), but for some reason the uiview's position is also changing. I don't understand why its changing position when it changes rotation because I don't attempt to change position at all. Isn't rotation supposed to rotate the uiview relative to the center of the uiview?

Here is what is looks like: https://www.youtube.com/watch?v=5M1L2MyPdbg&feature=youtu.be

Here is my code:

- (void)rotateHand:(UIPanGestureRecognizer *)panGesture {
    if ([(UIPanGestureRecognizer*)panGesture state] == UIGestureRecognizerStateBegan) {
        CGPoint touchPoint = [panGesture locationInView:[self view]];
        float dx = touchPoint.x - minHandContainer.center.x;
        float dy = touchPoint.y - minHandContainer.center.y;
        arcTanMin = atan2(dy,dx);
        startTransform = minHandContainer.transform;
    }
    if ([(UIPanGestureRecognizer*)panGesture state] == UIGestureRecognizerStateChanged) {
        CGPoint pt = [panGesture locationInView:[self view]];
        float dx = pt.x  - minHandContainer.center.x;
        float dy = pt.y  - minHandContainer.center.y;
        float ang = atan2(dy,dx);
        float angleDifference = arcTanMin - ang;
        minHandContainer.transform = CGAffineTransformRotate(startTransform, -angleDifference);
    }
}

Upvotes: 0

Views: 791

Answers (1)

Kevin
Kevin

Reputation: 302

There are a couple things that might be happening. First, if you have autolayout on, it might be doing something weird to you rotation, like trying to maintain the boundaries between the UIView and the sides of the device during the rotation, as discussed in this thread. Try switching it off and testing if it is currently on.

If that's not the case, you might want to take a look at this thread.

Upvotes: 1

Related Questions