【问题标题】:UIPanGestureRecognizer on a UIView behaves oddly after UIViews layer is animatedUIViews 层动画后,UIView 上的 UIPanGestureRecognizer 行为异常
【发布时间】:2013-09-18 22:40:32
【问题描述】:

我有一系列 UIView,我沿着弧线制作动画。每个视图上都有一个 UIPanGestureRecognizer,它在任何动画发生之前都会按预期工作。

但是,在为视图设置动画后(使用下面的方法为视图层设置动画),手势无法按预期工作;事实上,它们看起来好像在它们原来的屏幕位置上工作。

我尝试了各种方法,包括将视图框架和/或边界与动画层上的相匹配,甚至删除现有手势并再次创建和添加它们(未在下面显示)。

如果有人知道发生了什么或者可以让我解决问题,将不胜感激。

编辑: iMartin 的改变建议

pathAnimation.removedOnCompletion = NO;

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;
        }
    }];

}

【问题讨论】:

    标签: ios objective-c uiview uigesturerecognizer caanimation


    【解决方案1】:

    动画后你检查视图/图层的帧了吗?

    我认为问题在于,你所看到的不是你得到的图层本身并没有改变它的位置/框架,只是它的presentationLayer 改变了。 表示层是你在屏幕上实际看到的。如果删除此行,您会看到:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-31
      • 2012-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      相关资源
      最近更新 更多