iphone - 如何使用 CABasicAnimation 对图层的框架进行动画处理?

标签 iphone core-animation

我想我必须将 CGRect 转换为一个对象才能将其传递给 fromValue?

这是我尝试的方法,但它不起作用:

CABasicAnimation *frameAnimation = [CABasicAnimation animationWithKeyPath:@"frame"];
frameAnimation.duration = 2.5;
frameAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
frameAnimation.fromValue = [NSValue valueWithCGRect:myLayer.frame];
frameAnimation.toValue = [NSValue valueWithCGRect:theNewFrameRect];
[myLayer addAnimation:frameAnimation forKey:@"MLC"];

最佳答案

CALayer 的框架属性是派生属性,依赖于图层的位置、 anchor 、边界和变换。您不应该对框架进行动画处理,而应该对位置或边界进行动画处理,具体取决于您想要实现的效果。

要移动图层,您可以设置位置的动画:

-(void)moveLayer:(CALayer*)layer to:(CGPoint)point
{
    // Prepare the animation from the current position to the new position
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    animation.fromValue = [layer valueForKey:@"position"];

    // NSValue/+valueWithPoint:(NSPoint)point is available on Mac OS X
    // NSValue/+valueWithCGPoint:(CGPoint)point is available on iOS
    // comment/uncomment the corresponding lines depending on which platform you're targeting

    // Mac OS X
    animation.toValue = [NSValue valueWithPoint:NSPointFromCGPoint(point)];
    // iOS
    //animation.toValue = [NSValue valueWithCGPoint:point];

    // Update the layer's position so that the layer doesn't snap back when the animation completes.
    layer.position = point;

    // Add the animation, overriding the implicit animation.
    [layer addAnimation:animation forKey:@"position"];
}

要调整图层大小,您可以为 bounds 参数设置动画:

-(void)resizeLayer:(CALayer*)layer to:(CGSize)size
{
    // Prepare the animation from the old size to the new size
    CGRect oldBounds = layer.bounds;
    CGRect newBounds = oldBounds;
    newBounds.size = size;
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds"];

    // NSValue/+valueWithRect:(NSRect)rect is available on Mac OS X
    // NSValue/+valueWithCGRect:(CGRect)rect is available on iOS
    // comment/uncomment the corresponding lines depending on which platform you're targeting

    // Mac OS X
    animation.fromValue = [NSValue valueWithRect:NSRectFromCGRect(oldBounds)];
    animation.toValue = [NSValue valueWithRect:NSRectFromCGRect(newBounds)];
    // iOS
    //animation.fromValue = [NSValue valueWithCGRect:oldBounds];
    //animation.toValue = [NSValue valueWithCGRect:newBounds];

    // Update the layer's bounds so the layer doesn't snap back when the animation completes.
    layer.bounds = newBounds;

    // Add the animation, overriding the implicit animation.
    [layer addAnimation:animation forKey:@"bounds"];
}

如果您需要同时移动图层并调整图层大小,则可以使用 CAAnimationGroup 组合这些动画。

关于iphone - 如何使用 CABasicAnimation 对图层的框架进行动画处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2892888/

相关文章:

iphone - 获取包含在 NSString 中的文件的扩展名

ios - animateWithDuration 无法识别?

iphone - Open GL 是 2d 纸牌游戏的矫枉过正吗?

iphone - 3D立方体问题!第1部分

iphone - 删除 NSMutableDictionary 中索引 20 之后的所有对象?

iphone - 变量被编译器优化掉

ios - 如何实现一个动画队列,一个动画又一个动画结束

ios - 如何使用CATransform3DMakeRotation?

iphone - 你如何在 didSelectRowAtIndexPath 中使用 for 循环来最小化我的代码?

iphone - 在 iPhone 上下载图片 - Facebook 是如何做到的?