Rajveer
Rajveer

Reputation: 857

Animate UIView while being dragged

I'm trying to animate a UIView expanding when the user begins dragging it, and animate it collapsing when they stop dragging. In iOS5 this seems to work, although not perfectly - the view animates moving to the current finger position while expanding (so lags), but works fine when the expanding animation finishes.

In iOS4 it's just broken - the view animates expanding, but doesn't move at all with the user's finger until animation finishes.

What can I do?

- (void)gestureRecognizerMoved:(id)sender
{
    [self.view bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
    CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
    NSLog(@"translated x %f y %f", translatedPoint.x, translatedPoint.y);

    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan)
        {
        firstX = [sender view].center.x;
        firstY = [sender view].center.y;

        NSLog(@"first x %f y %f", firstX, firstY);
    }

    translatedPoint = CGPointMake([sender view].center.x, firstY+translatedPoint.y);
    [[sender view] setCenter:translatedPoint];

    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan)
    {
        [UIView animateWithDuration:0.8f animations:^(void)
         {
             CGRect frame = [sender view].frame;
             frame.size.width += 200.0f;
             [sender view].frame = frame;
         }];
    }

    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
    {
        [UIView animateWithDuration:0.8f animations:^(void)
         {
             CGRect frame = [sender view].frame;
             frame.size.width -= 200.0f;
             [sender view].frame = frame;
         }];
    }
}

EDIT: Using a Core Graphics affine transformation to resize the view (e.g. CGAffineTransformMakeScale) rather than modifying the view's frame gets rid of the lag in iOS5, so now it sticks to the user's finger press even during animation, rather than animating to the press location, perfect! However still getting the same issue in iOS4.

Upvotes: 0

Views: 1559

Answers (2)

Kaan Dedeoglu
Kaan Dedeoglu

Reputation: 14841

Try using the UIViewAnimationOptionAllowUserInteraction option, and it should work :)

Upvotes: 0

Sulthan
Sulthan

Reputation: 130102

You should probably include UIViewAnimationOptionBeginFromCurrentState to your animation options.

Edit:

You should use UIViewAnimationOptionBeginFromCurrentState because your gesture can end before the animation ends.

Chaging view center is just another way of changing view frame. The two properties change the same data. If you are animating the frame size and then you change the frame directly via center (overwriting what the animation does), you won't get what you desire.

The easist solution I see is putting your view into a transparent container and change the position of the container while the view can still animate its size.

Upvotes: 1

Related Questions