Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 卡莱尔动画问题_Ios_Animation_Calayer - Fatal编程技术网

Ios 卡莱尔动画问题

Ios 卡莱尔动画问题,ios,animation,calayer,Ios,Animation,Calayer,我正在尝试为不同的Calayer制作动画,但只有最后的动画可以工作 我这样做是为了创建包含文本的不同Calayer,并为每个Calayer添加动画。以下是生成CALayers的代码: // Create a layer for the title CALayer *_watermarkLayer = [CALayer layer]; [_watermarkLayer setOpacity:0]; // Create a layer for the text of

我正在尝试为不同的Calayer制作动画,但只有最后的动画可以工作

我这样做是为了创建包含文本的不同Calayer,并为每个Calayer添加动画。以下是生成CALayers的代码:

    // Create a layer for the title
    CALayer *_watermarkLayer = [CALayer layer];
    [_watermarkLayer setOpacity:0];

    // Create a layer for the text of the title.
    CATextLayer *titleLayer = [CATextLayer layer];
    titleLayer.string = text;
    titleLayer.foregroundColor = [color CGColor];
    titleLayer.shadowOpacity = 0.5;
    titleLayer.alignmentMode = kCAAlignmentCenter;
    titleLayer.bounds = CGRectMake(0, 0, videoSize.width/2, videoSize.height/2); 
    [_watermarkLayer addSublayer:titleLayer];

    // Fade in/out animation
    NSString* aux = [NSString stringWithFormat:@"%d", seconds];
    CABasicAnimation *fadeAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    fadeAnimation.fromValue = [NSNumber numberWithFloat:1.0];
    fadeAnimation.toValue = [NSNumber numberWithFloat:0.0];
    fadeAnimation.additive = YES;
    fadeAnimation.removedOnCompletion = YES;
    fadeAnimation.beginTime = seconds;
    fadeAnimation.duration = 2.0;
    fadeAnimation.fillMode = kCAFillModeRemoved; 
    [_watermarkLayer addAnimation:fadeAnimation forKey:[@"animateOpacity" stringByAppendingString:aux]];
我使用的是从上述代码中获得的CALayer,如下所示:

- (void) addWatermarkWithVideoComposition:(AVMutableVideoComposition*)videoComposition withLabel:(NSString*)text andColor:(UIColor*)color andBeginTimeInSeconds:(int)seconds
{
    // Setup video layers
    CALayer *parentLayer = [CALayer layer];
    CALayer *videoLayer = [CALayer layer];
    parentLayer.frame = CGRectMake(0, 0, videoComposition.renderSize.width, videoComposition.renderSize.height);
    videoLayer.frame = CGRectMake(0, 0, videoComposition.renderSize.width, videoComposition.renderSize.height);
    [parentLayer addSublayer:videoLayer];

    // Create and add watermark layer
    CALayer *exportWatermarkLayer = [self watermarkLayerForSize:CGSizeMake(300, 300) andText:text andColor:color andBeginTimeInSeconds:seconds];
    exportWatermarkLayer.position = CGPointMake(videoComposition.renderSize.width/2, videoComposition.renderSize.height/4);
    [parentLayer addSublayer:exportWatermarkLayer];

    // Merge layers
    videoComposition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];
}
用法:

AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
    videoComposition.instructions = instructions;
    videoComposition.renderSize = outputSize;
    videoComposition.frameDuration = CMTimeMake(1, 30);

    [self addWatermarkWithVideoComposition:videoComposition withLabel:@"Tag" andColor:lastColor andBeginTimeInSeconds:0];
    [self addWatermarkWithVideoComposition:videoComposition withLabel:@"Tag" andColor:lastColor andBeginTimeInSeconds:3];
    [self addWatermarkWithVideoComposition:videoComposition withLabel:@"Tag" andColor:lastColor andBeginTimeInSeconds:5];

AVAssetExportSession *exporter = [AVAssetExportSession exportSessionWithAsset:composition presetName:preset];
....................

我做错了什么

据我所知,您的代码中有三个问题。您可能希望:

  • 更改
    fadeAnimation.removedOnCompletion=YES
    …=没有

  • 更改
    fadeAnimation.fillMode=xyz
    =
    kCAFillModeForwards

  • 更改所有Calayer的
    .beginTime
    ,以及所有 您希望使用AVVideoCompositionCoreAnimationTool导出 当前有
    beginTime==0.0
    (这是默认值!!)到
    avcoreAnimationBeginTimetZero

  • 关于这一点的更多细节可以在苹果公司的文档中找到

    协议中beginTime属性的Apples文档中没有提到
    beginTime
    提示是不好的


    我有一个类似的问题,只有那些动画工作,我显式地设置了一个(非零)开始时间。希望这也能解决您的问题。

    我使用以下代码创建一个水印
    CALayer
    到视频中心,它在osx
    Cocoa
    上工作。希望这会有帮助

    - (CALayer *)createLayerImage:(NSString *)imagePath videoSize:(CGSize)videoSize
    {
        CALayer *watermarkLayer = [CALayer layer];
        NSImage *watermarkImage = [[NSImage alloc] initWithContentsOfFile:imagePath];
        [watermarkLayer setContents:watermarkImage];
        [watermarkLayer setFrame:CGRectMake((videoSize.width-watermarkImage.size.width)/2,
                                            (videoSize.height-watermarkImage.size.height)/2,
                                            watermarkImage.size.width,
                                            watermarkImage.size.height)];
        NSLog(@"watermark layer %fx%f", watermarkImage.size.width, watermarkImage.size.height);
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
        animation.fromValue = [NSNumber numberWithFloat:1.0];
        animation.toValue = [NSNumber numberWithFloat:1.0];
        animation.removedOnCompletion = NO;
        animation.duration = 5.0;
        animation.repeatCount = 1;
        animation.beginTime = AVCoreAnimationBeginTimeAtZero;
        animation.fillMode = kCAFillModeForwards;
        [watermarkLayer addAnimation:animation forKey:@"opacity"];
    
        return watermarkLayer;
    }
    

    据我所知,代码中只有一个图层和动画对不起,现在更新了代码。