【问题标题】:How to use UIBezierPath with CoreAnimation?如何将 UIBezierPath 与 CoreAnimation 一起使用?
【发布时间】:2012-02-26 08:43:39
【问题描述】:

我正在使用此代码将我的 UIView 删除为按下删除按钮时的删除按钮。 这是代码:

UIBezierPath *movePath = [UIBezierPath bezierPath];
            [movePath moveToPoint:icon.center];
            [movePath addQuadCurveToPoint:senderView.center
                             controlPoint:CGPointMake(senderView.center.x, icon.center.y)];

            CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
            moveAnim.path = movePath.CGPath;
            moveAnim.removedOnCompletion = YES;

            CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
            scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
            scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)];
            scaleAnim.removedOnCompletion = YES;

            CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"alpha"];
            opacityAnim.fromValue = [NSNumber numberWithFloat:1.0];
            opacityAnim.toValue = [NSNumber numberWithFloat:0.1];
            opacityAnim.removedOnCompletion = YES;

            CAAnimationGroup *animGroup = [CAAnimationGroup animation];
            animGroup.animations = [NSArray arrayWithObjects:moveAnim, scaleAnim, opacityAnim, nil];
            animGroup.duration = 2.0;
            [icon.layer addAnimation:animGroup forKey:nil];

这实际上将 UIView 设置为删除按钮,但在它再次出现在同一个地方之后。

【问题讨论】:

    标签: iphone objective-c xcode core-animation


    【解决方案1】:

    动画应用于表示层,而不是模型层。您需要将更改应用于两者。这是一种奇特的说法,当你做动画时,你也需要直接设置它。

            ...
            [icon.layer setPosition:CGPointMake(senderView.center.x, icon.center.y)];
            [icon.layer setTransform:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
            [icon.layer setAlpha:0.1];
    

    (请注意,您可能希望在此处为视图的center 设置动画,而不是图层的position。)

    iOS 5 Programming Pushing the Limits 的第 7 章有很多页面,如果您想了解所有血腥细节。这是removedOnCompletion的部分:

    有时您会看到人们建议将 removedOnCompletion 设置为 NO,将 fillMode 设置为 kCAFillModeBoth。这不是一个好的解决方案。它本质上使动画永远持续下去,这意味着模型层永远不会更新。如果您询问房产的价值,您将继续看到模型价值,而不是您在屏幕上看到的内容。如果您之后尝试隐式地为该属性设置动画,它将无法正常工作,因为 CAAnimation 仍在运行。如果您曾经通过将动画替换为另一个具有相同名称的动画来移除动画,调用 removeAnimationForKey: 或 removeAllAnimations,旧值会快速恢复。最重要的是,它会浪费内存。

    【讨论】:

      【解决方案2】:

      将每个动画的removedOnCompletion 设置为NO,将其fillMode 设置为kCAFillModeForwards

      【讨论】:

      • 试过了,但动画结束后 UIView 仍然显示。
      • 嗯。也许动画组需要相同的设置?它可能会覆盖其子动画的属性。
      • 到处都做同样的事情。就像它停在它开始的地方一样。
      【解决方案3】:

      尝试将不透明度动画更改为:

      CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];

      然后像 Noah Witherspoon 建议的那样,将动画组的 removeOnCompletion 设置为 NO 并将其 fillMode 设置为 kCAFillModeForwards

      【讨论】:

        最近更新 更多