繁体   English   中英

移动CALayer的动画?

[英]Moving Animation of a CALayer?

我正在循环并尝试移动一系列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];
        }

您有两个问题:第一个问题是您试图直接为图层的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];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM