使用CALayer for iphone创建ken烧伤效果时遇到问题

时间:2012-02-25 09:49:48

标签: iphone ios core-animation avfoundation calayer

过去几天我一直在尝试使用带有动画的CALayer创建ken burns效果,然后将其保存到视频文件中。

我的图像层位于另一个1024x576的图层内。所有动画都应用于图像层。

以下是目前的代码:

- (CALayer*)buildKenBurnsLayerWithImage:(UIImage *)image startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint fromScale:(float)fromScale toScale:(float)toScale
{
    float calFromScale = fromScale + 1;
    float calToScale = toScale + 1;
    float fromX = startPoint.x * calFromScale;
    float fromY = (image.size.height * calFromScale) - (videoSize.height + (startPoint.y * calFromScale));
    float toX = endPoint.x * calToScale;
    float toY = (image.size.height * calToScale) - (videoSize.height + (endPoint.y * calToScale));

    CGPoint anchor = CGPointMake(0.0, 0.0);

    CALayer* imageLayer    = [CALayer layer];
    imageLayer.contents    = (id)image.CGImage;
    imageLayer.anchorPoint = anchor;
    imageLayer.bounds      = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    imageLayer.position    = CGPointMake(image.size.width * anchor.x, image.size.height * anchor.y);
    imageLayer.contentsGravity = kCAGravityResizeAspect;

    // create the panning animation.
    CABasicAnimation* panningAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    panningAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(-fromX, -fromY)];
    panningAnimation.toValue   = [NSValue valueWithCGPoint:CGPointMake(-toX, -toY)];
    panningAnimation.additive = YES;
    panningAnimation.removedOnCompletion = NO;

    // create the scale animation.
    CABasicAnimation* scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.fromValue = [NSNumber numberWithFloat:fromScale];
    scaleAnimation.toValue = [NSNumber numberWithFloat:toScale];
    scaleAnimation.additive = YES;
    scaleAnimation.removedOnCompletion = NO;

    CAAnimationGroup* animationGroup = [CAAnimationGroup animation];
    animationGroup.animations = [[NSMutableArray alloc] initWithObjects:panningAnimation,scaleAnimation, nil];
    animationGroup.beginTime = 1e-100;
    animationGroup.duration = 5.0;
    animationGroup.removedOnCompletion = NO;
    animationGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

    [imageLayer addAnimation:animationGroup forKey:nil];

    return imageLayer;
}

以下是我如何调用该方法:

CALayer* animatedLayer = [self buildKenBurnsLayerWithImage:image startPoint:CGPointMake(100, 100) endPoint:CGPointMake(500, 500) fromScale:5.0 toScale:2.0];

我遇到的问题是平移和缩放的最终结果是屏幕上的几个像素。

如果有人知道如何解决这个问题,我将非常感激。

1 个答案:

答案 0 :(得分:0)

相对于锚点应用所有变换。尝试使用锚点

CGPoint anchor = CGPointMake(0.5f, 0.5f);

您的“视口”不应再缩放到右下角(如果这是导致动画偏离几个像素的原因),但同样适用于所有方向。