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.
Try using the UIViewAnimationOptionAllowUserInteraction option, and it should work 🙂