【问题标题】:Moving Animation of a CALayer?CALayer 的移动动画?
【发布时间】:2011-11-05 12:35:26
【问题描述】:

我有一个 CALayers 数组,我正在循环并试图移动它们。

Tile 是一个 CALayer 子类,并且有一个名为 originalFrame 的 CGRect 属性,我在其中存储了我想要制作动画的帧。

当我使用下面的代码时,所有内容都会立即移动到正确的位置,并且没有 4 秒动画。如何为这些 CALayer 制作动画?

       for (int i = 0; i < [tileArray count]; i++) {
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
            [UIView setAnimationDelay:i];
            [UIView setAnimationDuration:4];
            Tile *currentCard = (Tile*)[tileArray objectAtIndex:i];
            currentCard.frame = currentCard.originalFrame;
            [UIView commitAnimations];
        }

【问题讨论】:

    标签: ios objective-c ipad ios4


    【解决方案1】:

    您有两个问题:第一个是您试图直接为图层的frame 设置动画。由于这是派生属性,因此您不能这样做。相反,您必须为 position 属性设置动画。 http://developer.apple.com/library/mac/#qa/qa1620/_index.html

    其次,您使用的是 UIView 的 +beginAnimations API,但您说您的 Tile 对象是 CALayers,而不是 UIViews。所以你不需要使用+beginAnimations。相反,您需要使用 CAAnimation 对象,例如 CABasicAnimation(未经测试):

    for (Tile *tile in tileArray)
    {
        static NSString * const kProperty = @"position";
    
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:kProperty];
        animation.duration = 4.0f;
        animation.fromValue = [tile valueForKey:kProperty];
        animation.toValue = [NSValue valueWithCGRect:tile.originalFrame];
        [tile addAnimation:animation forKey:kProperty];
    }
    

    【讨论】:

    • 谢谢,正是我想要的!
    猜你喜欢
    • 2012-01-09
    • 2010-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-15
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    相关资源
    最近更新 更多