Animate AVPlayerLayer videoGravity property
Estou tentando copiar o comportamento da Apple na reprodução de vídeo que permite ao usuário esticar a imagem de vídeo para preencher os limite
@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];
}
}
}
Isso funciona muito bem quando tento animar uma propriedade numérica, como opacidade. Mas você notará, a partir da referência da classe, que a propriedade videoGravity do AVPlayerLayer é uma NSString. A referência da classe indica que é animavel.
Então minha pergunta é: Como!?
Creio que isso provavelmente esteja relacionado de alguma forma à propriedade de conteúdo do CALayer e, possivelmente, ao contentGravit