2012-05-09 97 views
1

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

现在,我需要对anchorPoint进行动画处理,但在所有尝试中都失败了!我这样做,即使:

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轴waaaay太远的图层,然后回来之前..和重复(因为自动重新打开)。

关于图层层次结构的注意事项: 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,相同的结果:(

最后一个音符,我写一个可可应用程序,但动画的X值应为正是在两个可可和可可触摸你是不是要去一样。

+1

如果我可能会对您的代码本身发表评论,它很好,很干净 –

+1

谢谢@JamesGraham!我主要与那些在编码时看不到这种艺术价值的开发者合作:/ – Mazyod

+0

阅读:)是一种快乐:) –

回答

2

相信这(好像我跟你从一开始就坐在)..

动画宝并且旋转工作正常,没有设置fromValue。但是,动画定位点需要我将定位点fromValue设置为初始值!

animation.fromValue = [subLayer valueForKeyPath:keyPath]; 
+1

这是奇怪的。它几乎听起来像一个CA错误。您可能希望使用Apple的雷达缺陷跟踪系统记录错误报告。苹果通常会回应,如果它是一个错误,它会引起他们的注意,如果不是,你会学到为什么它会这样工作。 –

+0

哇,我从来没有使用过雷达,从来不知道他们会以这种方式回应。这绝对值得一试。 – Mazyod