【问题标题】:Animating CALayer anchorPoint动画 CALayer 锚点
【发布时间】:2012-05-09 06:04:43
【问题描述】:

我已成功为图层的位置和变换(旋转)设置动画。

现在,我需要为锚点设置动画,但所有尝试都失败了!即使我这样做:

2012... App[6437:403] layer: 1, KeyPath: anchorPoint, fromValue: NSPoint: {1.05, 0.5}, toValue: NSPoint: {1.05, 0.5}, Duration: 2.5

这是在为图层设置动画之前卡住的 NSLog。我得到的结果是图层在 X 轴上向左移动太远,然后又回来……并重复(因为自动反转已打开)。

关于图层层次结构的注意事项: SuperLayer 有三个子层,我将在其上应用动画。

为什么?锚点应该如何动画化?

我的代码:

    NSArray* subKeyPath     = [keyPaths objectAtIndex:tag];
    NSArray* subToValue     = [toValues objectAtIndex:tag];
    NSArray* subDuration    = [durations objectAtIndex:tag];

    NSEnumerator* keyPathsEnum  = [subKeyPath objectEnumerator];
    NSEnumerator* toValuesEnum  = [subToValue objectEnumerator];
    NSEnumerator* durationsEnum = [subDuration objectEnumerator];

    NSString* keyPath;
    NSNumber* toValue;
    NSNumber* duration;

    while ((keyPath = [keyPathsEnum nextObject]) && (toValue = [toValuesEnum nextObject]) && (duration = [durationsEnum nextObject])) {
        CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:keyPath];

        id offsetToValue;
        if ([keyPath hasPrefix:@"position"]) {
        //I removed the position animation, since animating the anchorPoint should suffice
        } else if ([keyPath hasPrefix:@"anchorPoint"]) {
            //This was when I used anchorPoint.x float value         = [[subLayer valueForKeyPath:keyPath] floatValue] - ([toValue floatValue]/[[subLayer contents] size].width);
            offsetToValue       = [NSNumber valueWithPoint:CGPointMake(1.05, 0.5)];
            keyPath             = [keyPath substringToIndex:[keyPath length]-2];
        } else {
            offsetToValue       = toValue;
        }

...

        animation.toValue       = offsetToValue;
        animation.duration      = [duration floatValue];
        animation.fillMode      = kCAFillModeForwards;
        animation.repeatCount   = INFINITY;
        animation.autoreverses  = YES;
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

        [subLayer addAnimation:animation forKey:keyPath];

        //update the model

    }

我尝试为“anchorPoint.x”keyPath 设置动画,结果相同:(

最后一点,我正在编写一个 cocoa 应用程序,但是在 cocoa 和 cocoa touch 上设置 X 值的动画应该完全相同。

【问题讨论】:

  • 如果我可以评论您的代码本身,那就太好了
  • 谢谢@JamesGraham!我主要与在编码时看不到此类艺术价值的开发人员一起工作:/
  • 阅读是一种乐趣:)

标签: ios cocoa core-animation transform calayer


【解决方案1】:

你不会相信的(好像我从一开始就和你坐在一起)..

在不设置fromValue 的情况下,动画位置和旋转工作正常。但是,为锚点设置动画需要我将锚点fromValue 设置为初始值!

animation.fromValue = [subLayer valueForKeyPath:keyPath];

【讨论】:

  • 这很奇怪。这几乎听起来像一个 CA 错误。您可能希望使用 Apple 的 Radar 错误跟踪系统记录错误报告。 Apple 通常会做出回应,如果是错误,它会引起他们的注意,如果不是,您将了解为什么它会这样工作。
  • 哇,我从没用过雷达,也不知道他们会这样回应。绝对值得一试。
猜你喜欢
  • 2011-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-30
  • 2017-11-25
  • 2011-11-05
相关资源
最近更新 更多