
UIView 可以设定 autoresizingMask,当它的 superView 尺寸改变时,适应何种变化。
不过 CALayer 却没有这个属性,如和做到让 CALayer 和 UIView 一样?可以这样:
// in ViewController
- (void)viewWillLayoutSubviews
{
_playerLayer.frame = self.view.bounds;
} // in View
- (void)layoutSublayersOfLayer:(CALayer *)layer
{
_playerLayer.frame = self.bounds;
}
这不是一个好的办法。
当当~
自定义一个 UIView,重写它的 + (Class)layerClass 方法,像这样
@interface RFPlayerLayerView : UIView @end @implementation RFPlayerLayerView + (Class)layerClass
{
return [AVPlayerLayer class];
} @end
使用时设置一些参数,就可以完美拥有 UIView 的 autoresizingMask 的属性了
- (void)setup
{
_layerView = [[RFPlayerLayerView alloc] initWithFrame:rect];
_playerLayer = (AVPlayerLayer *)[_layerView layer];
_playerLayer.player = _player;
_playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
_layerView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self.view addSubview:_layerView];
}