小编Nat*_*Sun的帖子

设置AVSynchonizedLayer

我一直在尝试制作一个将视频与动画对象连接起来的概念验证(实际上,我是将用户选择的位图"旋转"到特定位置的固定视频上).在观看了有关Core Animation(WWDC10等)和AVSync..Layer(Apple WWDC10示例代码,doc示例代码)的所有可用Apple资料后,根本没有足够明确的隔离代码示例来推导如何实现此正确播放模式.

使用CA,我有一个加载到CALayer中的视频资源,以及一个附加到位图的动画,然后连接到另一个CALayer.我创建了一个AVSynchronizedLayer并将这两个对象添加为子层.

当这个树被添加到UIView的图层属性时,我期望动画的播放与视频同步运行.相反,我只看到视频播放,动画永远不会执行 - 这里是我的代码的相关部分,它们都位于一个View Controller中(在ViewDidLoad中调用).

Video101.m4v =简单的m4v视频,gerald.png =位图

NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"Video101" withExtension:@"m4v"];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:fileURL options:nil];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];

AVPlayer *player = [[AVPlayer playerWithPlayerItem:playerItem]retain];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame = CGRectMake(0, 0, 1024, 768);
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

CALayer *gerald = [CALayer layer];
gerald.frame = CGRectMake(0, 0, 120, 120);
gerald.contents = (id) [UIImage imageNamed:@"gerald.png"].CGImage;

[self.view.layer addSublayer:playerLayer];
[self.view.layer insertSublayer:gerald above:playerLayer];


CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
anim.duration = 18.0;

anim.values = [NSArray …
Run Code Online (Sandbox Code Playgroud)

video animation core-animation sync ios

12
推荐指数
1
解决办法
3210
查看次数

标签 统计

animation ×1

core-animation ×1

ios ×1

sync ×1

video ×1