通过UIPanGestureRecognizer很好地移动视图,但有时会跳得很厉害

时间:2012-02-27 06:12:00

标签: iphone objective-c ios uipangesturerecognizer

我正在通过此代码移动视图,它移动得很好,但有时它们非常跳跃,对此有任何想法吗?

[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];

[self.view bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];

if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {

    firstX = [[sender view] center].x;
    firstY = [[sender view] center].y;
}

translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);

[[sender view] setCenter:translatedPoint];

if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

    CGFloat finalX = translatedPoint.x + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x);
    CGFloat finalY = translatedPoint.y + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);


[UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.35];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [[sender view] setCenter:CGPointMake(finalX, finalY)];
    [UIView commitAnimations];

1 个答案:

答案 0 :(得分:1)

看起来上面代码中的任何内容都不会减慢平移速度。 UI上任何缓慢的响应都是重新绘制造成的99%的时间。对于您正在平移的视图,请查看drawRect代码(如果已实现它)。如果每次平移都会调用此命令,这可能会导致问题。您可以通过在视图中将rasterize属性设置为YES来解决此问题。