是否可以在 CAKeyFrameAnimation 之后更新按钮的位置?

Sam*_*D R 1 animation objective-c ios

我一直在尝试在完成后更新按钮的位置CAKeyFrameAnimation。到目前为止,我已经在弧上的某个角度(最终角度)停止了动画,但是,按钮所进行的触摸似乎是在动画发生之前的那个点。

这是我一直在使用的代码:

for(int i = 0; i <numberOfButtons; i++)
{
    double angle = [[self.buttonsArray objectAtIndex:i] angle];

    if(angle != -50 && angle !=270){
        CGPoint arcStart = CGPointMake([[self.buttonsArray objectAtIndex:i] buttonForCricle].center.x, [[self.buttonsArray objectAtIndex:i] buttonForCricle].center.y);
        CGPoint arcCenter = CGPointMake(centerPoint.x, centerPoint.y);
        CGMutablePathRef arcPath = CGPathCreateMutable();
        CGPathMoveToPoint(arcPath, NULL, arcStart.x, arcStart.y);
        CGPathAddArc(arcPath, NULL, arcCenter.x, arcCenter.y, 150, ([[self.buttonsArray objectAtIndex:i] angle]*M_PI/180), (-50*M_PI/180), YES);
        UIButton *moveButton = (UIButton *)[self.view viewWithTag:i+1];
        CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        pathAnimation.calculationMode = kCAAnimationPaced;
        pathAnimation.fillMode = kCAFillModeForwards;
        // pathAnimation.values = @[@0];
        pathAnimation.removedOnCompletion = NO;
        pathAnimation.duration = 1.0;
        pathAnimation.path = arcPath;
        CGPathRelease(arcPath);

        //  UIView* drawArcView = nil;
        //  drawArcView = [[UIView alloc] initWithFrame: self.view.bounds];
        //  CAShapeLayer* showArcLayer = [[CAShapeLayer alloc] init];
        //  showArcLayer.frame = drawArcView.layer.bounds;
        //  showArcLayer.path = arcPath;
        //  showArcLayer.strokeColor = [[UIColor blackColor] CGColor];
        //  showArcLayer.fillColor = nil;
        //  showArcLayer.lineWidth = 1.0;
        //  [drawArcView.layer addSublayer: showArcLayer];
        //  [self.view addSubview:drawArcView];

        [moveButton.layer addAnimation:pathAnimation forKey:@"arc"];
        [CATransaction commit];
        [self.view bringSubviewToFront:moveButton];

    }  

}
Run Code Online (Sandbox Code Playgroud)

在此之后如何更新按钮位置?

Dav*_*ist 5

为什么会发生这种情况

您所看到的是动画仅更改图层的“表示值”,而不会更改“模型值”。除此之外,动画在完成时默认会被删除,并且您可以解释为什么按钮会在动画完成后返回其原始位置。

这是实际按钮的状态,以及当用户点击它时被点击测试的位置。

的组合removedOnCompletion = NOfillMode = kCAFillModeForwards使它看起来像按钮移动到它的最终位置,但它实际上是造成持续存在呈现值和模型值之间的差异。

简而言之,按钮看起来像是在一个位置,但实际上在另一个位置。

如何修复

对于这种情况,我建议让动画完成后移除,并且不要指定任何特殊的填充模式,然后解决按钮位置不正确的问题。

他们这样做的方法是将按钮位置/中心设置为其最终值。这将使按钮的模型位置立即更改,但在动画期间其呈现位置保持不变。动画完成并被移除后,按钮将返回其模型值,该值与动画的最终值相同。所以按钮看起来和都在正确的位置。


通常更新模型值最简单的地方是在创建动画对象之后但在将其添加到图层之前。

更改位置创建并添加隐式动画。但是只要显式动画(您正在创建的动画)更长,隐式动画就不会可见。

如果您不希望创建此隐式动画,则可以CATransansaction仅在更新位置属性的行周围创建一个,并禁用操作(请参阅 参考资料setDisableActions:)。


如果您想了解更多信息,我有一篇关于图层如何处理多个动画的博客文章以及有关何时更新图层的问题的答案