是否可以像使用CALayers一样将SKSpriteNode与视频同步?

时间:2014-09-21 03:52:34

标签: objective-c sprite-kit calayer skspritenode

我想将SKSpriteNode的动作与视频同步。因此,当视频向前搜索时,向后搜索或仅相应地播放SKSpriteNode移动。 我知道这可以通过CALayers轻松完成,但可以使用SKSpriteNode代替CALayers来完成吗?

// 1) Create a sync layer
AVPlayerItem * item = player.currentItem;
AVSynchronizedLayer* syncLayer = [AVSynchronizedLayer synchronizedLayerWithPlayerItem:item];
syncLayer.frame = CGRectMake(10, 220, 300, 10);
syncLayer.backgroundColor = [[UIColor lightGrayColor] CGColor];
[self.view.layer addSublayer:syncLayer];

// 2) Create a sublayer for the synclayer
CALayer *sublayer = [CALayer layer];
sublayer.frame = CGRectMake(0, 0, 200, 50); //our black box
sublayer.backgroundColor = [[UIColor blackColor] CGColor];
[syncLayer addSublayer:sublayer];

// 3) animate the layer!
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(924, 300)];
anim.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 300)];
anim.removedOnCompletion = NO;
anim.beginTime = AVCoreAnimationBeginTimeAtZero;  
anim.duration = CMTimeGetSeconds(item.asset.duration);

NSLog(@"%f",anim.duration);

[sublayer addAnimation:anim forKey:nil];

0 个答案:

没有答案