开发者

Using UIPanGestureRecognizer and CATransform3DScale to enlarge dragged element

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜