首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为CALayer anchorPoint设置动画

为CALayer anchorPoint设置动画
EN

Stack Overflow用户
提问于 2012-05-09 14:04:43
回答 1查看 1.2K关注 0票数 1

我已经成功地动画层的位置和变换(旋转)。

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

代码语言:javascript
复制
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有三个子层,我在上面应用动画。

为什么?应该如何设置锚点的动画?

我的代码:

代码语言:javascript
复制
    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,同样的结果是:(

最后注意,我正在编写一个可可应用程序,但动画的X值应该是完全相同的可可和可可触摸。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-09 15:01:41

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

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

代码语言:javascript
复制
animation.fromValue = [subLayer valueForKeyPath:keyPath];
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10510616

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档