UIView上的UIPanGestureRecognizer在UIView图层执行动画后表现异常

5
我有一系列的UIView,沿着一个弧线进行动画。每个视图上都有一个UIPanGestureRecognizer,在任何动画发生之前都能正常工作。
然而,在对视图进行动画后(使用下面的方法对视图的层进行动画),手势就不能如预期般工作了;实际上它们似乎在它们最初的屏幕位置上工作。
我尝试过各种方法,包括将视图的框架和/ 或边界与动画图层上的相匹配,甚至删除现有手势,重新创建并添加它们(未在下面显示)。
如果有人知道发生了什么或者可以让我解决这个问题,那将不胜感激。
编辑:iMartin的建议是更改
pathAnimation.removedOnCompletion = NO;

to

pathAnimation.removedOnCompletion = YES;

并且

view.layer.position = ... //

已经帮我走上解决问题的道路。
原始代码:
- (void)animateUIViewAlongArc:(UIView*)view clockwise:(BOOL)clockwise duration:(float)duration
{
    [UIView animateWithDuration:duration animations:^{

    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimationanimationWithKeyPath:@"position"];
    pathAnimation.calculationMode = kCAAnimationPaced;
    pathAnimation.fillMode = kCAFillModeForwards;
    pathAnimation.removedOnCompletion = NO;
    pathAnimation.repeatCount = 1;

    CGPoint viewCenter = view.objectCenter;
    CGMutablePathRef arcPath = CGPathCreateMutable();
    CGPathMoveToPoint(arcPath, NULL, viewCenter.x, viewCenter.y);

    float angleOfRotation = self.angleIncrement;
    if (clockwise == NO)
    {
        angleOfRotation *= -1.0f;
    }

    float fullwayAngle = view.angle + angleOfRotation;
    CGPoint fullwayPoint = pointOnCircle(self.center, self.radius, fullwayAngle);

    CGPathAddRelativeArc(arcPath, NULL, self.center.x, self.center.y, self.radius, view.angle, angleOfRotation);
    pathAnimation.path = arcPath;
    CGPathRelease(arcPath);

    [view.layer addAnimation:pathAnimation forKey:@"arc"];

    view.angle = fullwayAngle;
    view.objectCenter = fullwayPoint;

    } completion:^(BOOL finished){
        if (finished)
        {
            CGRect frame = view.layer.frame;
            CGRect bounds = view.layer.bounds;

            view.frame = frame;
            view.bounds = bounds;
        }
    }];

}

1个回答

3

您在动画后是否检查了视图/图层的框架?

我认为问题是,“你看到的并不是你所得到的”。 图层本身并没有改变其位置/框架,只是它的presentationLayer发生了变化。演示层是实际显示在屏幕上的内容。如果您删除这行代码,您可以看到这一点:

pathAnimation.removedOnCompletion = NO; // Delete or set to YES

将此设置为NO会导致演示层在屏幕上显示的位置与您的视图不同。
您应该做的是,在向演示层添加动画之前,将最终位置设置为该层

view.layer.position = finalPosition; // You should calculate this
[view.layer addAnimation:pathAnimation forKey:@"arc"];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接