【问题标题】:having trouble creating ken burns effect with CALayer for iphone无法使用 CALayer for iphone 创建 ken burns 效果
【发布时间】:2012-03-15 14:58:09
【问题描述】:

过去几天我一直在尝试使用带有动画的 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];

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

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

【问题讨论】:

  • 如果不缩放只平移,问题还会出现吗?

标签: iphone ios core-animation avfoundation calayer


【解决方案1】:

所有的变换都是针对锚点应用的。尝试使用锚点

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

您的“视口”不应再缩放到右下角(如果这是导致动画偏离几个像素的原因),而是在所有方向上均等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    相关资源
    最近更新 更多