我試圖複製Apple在視頻播放中的行爲,允許用戶伸展視頻圖像來填充邊界。Animate AVPlayerLayer videoGravity屬性
@interface FHVideoPlayerView : UIView
@end
@interface FHVideoPlayerView
+ (Class)layerClass
{
return [AVPlayerLayer class];
}
- (void)setAspectMode:(FHVideoPlayerAspectMode)aspectMode animated:(BOOL)animated
{
FHVideoPlayerAspectMode current = [self aspectMode];
FHVideoPlayerAspectMode final = aspectMode;
NSString *fromValue;
NSString *toValue;
AVPlayerLayer *layer = (AVPlayerLayer *)[self layer];
switch (current) {
case FHVideoPlayerAspectFill:
fromValue = AVLayerVideoGravityResizeAspectFill;
break;
case FHVideoPlayerAspectFit:
fromValue = AVLayerVideoGravityResizeAspect;
break;
default:
break;
}
switch (final) {
case FHVideoPlayerAspectFill:
toValue = AVLayerVideoGravityResizeAspectFill;
break;
case FHVideoPlayerAspectFit:
toValue = AVLayerVideoGravityResizeAspect;
break;
default:
break;
}
if (toValue != fromValue) {
if (animated == YES) {
// Manually added CABasicAnimation based on the understanding the implicit animations are disabled for CALayers that back a UIView
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"videoGravity"];
[layer addAnimation:animation forKey:@"animateVideoGravity"];
[CATransaction begin];
[CATransaction setAnimationDuration:0.333];
}
[layer setVideoGravity:toValue];
if (animated == YES) {
[CATransaction commit];
}
}
}
當我嘗試和動畫數值屬性,如不透明度這工作就好了。但是,您會從類參考中注意到,AVPlayerLayer的videoGravity屬性是一個NSString。類參考確實指出它是可以動畫的。
所以我的問題是:如何!?
我相信這可能與CALayer的內容屬性和可能的contentGravity有關。
所以我只是好奇,你有什麼與這些格式說明符?我知道你試圖混淆字符串,但你能解釋說明符嗎? – 2012-05-08 05:37:26
真棒解決方法!這在5.0發佈時給我帶來了很多麻煩,它最終在5.1中得到修復,但補充說修復是一個好主意。 – steipete 2012-05-08 08:24:12
哈哈'AVPlayerLayer'。我喜歡這個名字。 :) – 2012-05-08 08:26:05