gpt4 book ai didi

ios - 动画 AVPlayerLayer videoGravity 属性

转载 作者:可可西里 更新时间:2023-11-01 05:31:47 24 4
gpt4 key购买 nike

我正在尝试复制 Apple 在视频播放中的行为,允许用户拉伸(stretch)视频图像以填充边界。

@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 的内容属性以及可能的 contentsGravity 有关。

最佳答案

正如 voromax 所指出的,这是 iOS 5.0 中的一个错误。我对 iOS 5.1 中的 -[AVPlayerLayer setVideoGravity:] 实现进行了逆向工程,以了解动画应该如何工作。以下是如何解决该错误并在 iOS 5.0 上获得漂亮的动画。

@implementation VideoPlayerView

+ (Class) layerClass
{
return [AVPlayerLayer class];
}

- (AVPlayerLayer *) playerLayer
{
return (AVPlayerLayer *)[self layer];
}

- (NSString *) videoGravity
{
return self.playerLayer.videoGravity;
}

- (void) setVideoGravity:(NSString *)videoGravity
{
self.playerLayer.videoGravity = videoGravity;

// Workaround a bug in iOS 5.0
float avFoundationVersion = [[[NSBundle bundleForClass:[AVPlayerLayer class]] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey] floatValue];
if (avFoundationVersion < 292.24f)
{
@try
{
NSString *contentLayerKeyPath = [NSString stringWithFormat:@"%1$@%2$@.%3$@%2$@", @"player", [@"layer" capitalizedString], @"content"]; // playerLayer.contentLayer
CALayer *contentLayer = [self.playerLayer valueForKeyPath:contentLayerKeyPath];
if ([contentLayer isKindOfClass:[CALayer class]])
[contentLayer addAnimation:[CABasicAnimation animation] forKey:@"sublayerTransform"];
}
@catch (NSException *exception)
{
}
self.bounds = self.bounds;
}
}

@end

关于ios - 动画 AVPlayerLayer videoGravity 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8918105/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com