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

[[[(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];

最佳答案

似乎上面的代码中的任何内容都不会减慢平移。用户界面上任何缓慢的响应都是99%的时间由重画引起的。查看一下平移视图的drawRect代码(如果已实现)。如果每次平移时都调用此方法,则可能会导致您出现问题。您可以通过在视图中将rasterize属性设置为YES来解决此问题。

关于iphone - UIPanGestureRecognizer可以很好地移动 View ,但有时会跳动,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9460756/

10-10 08:19