views:

65

answers:

1

I want to implement a delayed 'enlarging' animation on an UIView that is being dragged using an attached UIPanGestureRecognizer. I've been using something similar to the following code:

UIView* innerView;
CGPoint startingPoint;

- (void)viewDidLoad { 
        innerView = [[[UIView alloc] autorelease] initWithFrame:CGRectMake(0, 0, 200, 200)];
    [[self view] addSubview:innerView];

    [innerView setBackgroundColor:[UIColor blueColor]];

    [innerView addGestureRecognizer:[[[UIPanGestureRecognizer alloc] autorelease] initWithTarget:self action:@selector(pan:)]];

    // resize it later
    [self performSelector:@selector(anim) withObject:nil afterDelay:1.5];
}

 -(void)pan:(UIPanGestureRecognizer*)gr {
    CGPoint gesturePoint = [gr translationInView:[self view]];

    if (gr.state == UIGestureRecognizerStateBegan)
            startingPoint = innerView.center;
    else
        innerView.center = CGPointMake(startingPoint.x + gesturePoint.x,         startingPoint.y +gesturePoint.y);
}

-(void)anim{
    innerView.layer.transform = CATransform3DMakeScale(1.5, 1.5, 1);
}

But the view 'jumps' when being dragged after the scale is applied. This behavior doesnt happen always, but in pseudo-random manner. Any one can shed light on this issue or propose a different approach?

+1  A: 

Try doing your transform on the view instead of the layer so that your -anim method looks like this:

-(void)anim
{
    [UIView beginAnimations:nil context:NULL];
    [innerView setTransform:CGAffineTransformMakeScale(1.5f, 1.5f)];
    [UIView commitAnimations];
}

The trouble is the root layer of a view has animations disabled by default so it will just snap to the transform you're setting.

Alternatively if you need to animate the layer, you can use an explicit animation using a CABasicAnimation.

Matt Long