xcode AVPlayerLayer 不绑定到 UIView 框架

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35443402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:33:01  来源:igfitidea点击:

AVPlayerLayer not bounds to UIView frame

iphonexcodeswiftavplayerlayercglayer

提问by Jornia Kabakum

I'm trying to add AVPlayerLayer to UIView

我正在尝试将 AVPlayerLayer 添加到 UIView

    self.player = AVPlayer(URL: NSURL(fileURLWithPath: path!))
    self.playerLayer = AVPlayerLayer(player: player);
    self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    self.playerLayer.frame =  ctrVideo.bounds;
    self.ctrVideo.layer.addSublayer(playerLayer);
    player.play();

This is the video's container (in blue):

这是视频的容器(蓝色):

This is the view:

这是视图:

Constraints: Constraints:

约束: 约束:

Final result: enter image description here

最后结果: 在此处输入图片说明

I can't figure out why video is not bounds to UIVIew coordinates. If i bounds it into controller's superview, it is ok.

我不明白为什么视频不受 UIVIew 坐标的限制。如果我将它绑定到控制器的超级视图中,就可以了。

回答by almas

Sublayer won't resize automatically when view's frame changes, you have to do it manually. You can subclass a UIView to make it easier.

当视图的框架更改时,子层不会自动调整大小,您必须手动进行。您可以将 UIView 子类化以使其更容易。

class VideoContainerView: UIView {
  var playerLayer: CALayer?
  override func layoutSublayersOfLayer(layer: CALayer) {
    super.layoutSublayersOfLayer(layer)
    playerLayer?.frame = self.bounds
  }
}

And then in your code:

然后在你的代码中:

self.player = AVPlayer(URL: NSURL(fileURLWithPath: path!))
self.playerLayer = AVPlayerLayer(player: player);
self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.playerLayer.frame =  ctrVideo.bounds;
self.ctrVideo.layer.addSublayer(playerLayer);
self.ctrVideo.playerLayer = playerLayer;
player.play();

Just make sure that you change that class of your "ctrVideo" from UIView to VideoContainerView.

只需确保将“ctrVideo”的该类从 UIView 更改为 VideoContainerView。

回答by Jornia Kabakum

I adding AVPlayerLayer in viewDidLoad event... Before constraints apply.

我在 viewDidLoad 事件中添加 AVPlayerLayer... 在约束应用之前。

回答by k06a

Here is AVPlayerLayer-backed view, that tries to keep its aspect ratio.

这是AVPlayerLayer支持视图,它试图保持其纵横比。

PlayerView.h:

播放器视图.h:

@interface PlayerView : UIView

@property (strong, nonatomic) AVPlayerLayer *layer;

@end

PlayerView.m:

PlayerView.m:

@interface PlayerView ()

@property (strong, nonatomic) NSLayoutConstraint *aspectConstraint;

@end

@implementation PlayerView

@dynamic layer;

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

- (instancetype)init {
    self = [super init];
    if (self) {
        self.userInteractionEnabled = NO;

        [self addObserver:self forKeyPath:@"layer.videoRect" options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}

- (void)dealloc {
    [self removeObserver:self forKeyPath:@"layer.videoRect"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"layer.videoRect"]) {
        CGSize size = [change[NSKeyValueChangeNewKey] CGRectValue].size;
        if (change[NSKeyValueChangeNewKey] && size.width && size.height) {
            self.aspectConstraint.active = NO;
            self.aspectConstraint = [self.widthAnchor constraintEqualToAnchor:self.heightAnchor multiplier:size.width / size.height];
            self.aspectConstraint.priority = UILayoutPriorityDefaultHigh;
            self.aspectConstraint.active = YES;
        }
        else {
            self.aspectConstraint.active = NO;
        }
    }
}

@end

回答by AnBisw

Here's @almas' awesome answer in Swift 4

这是@almas 在Swift 4 中的精彩回答

class VideoContainerView: UIView {    
    var playerLayer: CALayer?

    override func layoutSublayers(of layer: CALayer) {
        super.layoutSublayers(of: layer)
        playerLayer?.frame = self.bounds
    }
}