ios - 动画 CALayer anchorPoint

标签 ios cocoa core-animation transform calayer

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

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

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 有三个我正在应用动画的子层。

为什么? anchor 应该如何动画?

我的代码:

    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 值应该完全相同。

最佳答案

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

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

animation.fromValue = [subLayer valueForKeyPath:keyPath];

关于ios - 动画 CALayer anchorPoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10510616/

相关文章:

ios - 同步动画相互阻塞

objective-c - NSImageView 与 NSPopover 中的 NSTextField 相交时的动画伪像

ios - React-Native 中的 '...' 是什么意思?

ios - 隐藏单个 UITableViewCell 分隔符

ios - iOS App 中 AVAudioPlayer 和 MPMusicPlayerController 的独立音量控制。

macos - OS X 数据文件编辑器保存到 XML : Document based or not?

objective-c - 何时需要将 NSManagedObjectContext 与 NSMainQueueConcurrencyType 一起使用

iphone - UITableView刷新问题

objective-c - 在 Cocoa 中向窗口添加自定义 View

ios - 如何在 UIView 的 layerClass 实例上使用 CIFilter?