【问题标题】:Is it possible to update the position of a button after a CAKeyFrameAnimation?是否可以在 CAKeyFrameAnimation 之后更新按钮的位置?
【发布时间】:2017-03-16 17:57:38
【问题描述】:

在完成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];

    }  

}

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

【问题讨论】:

    标签: ios objective-c animation


    【解决方案1】:

    为什么会这样

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

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

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

    简单地说,按钮看起来在一个位置,但实际上它在另一个位置。

    如何解决

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

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


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

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

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


    如果你想了解更多,我有a blog post about how layers work with multiple animationsan answer to a question about when to update the layer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多