This commit is contained in:
2025-11-28 13:07:35 +08:00
parent c37038f163
commit 1a0a444a99

View File

@@ -8,6 +8,8 @@
#import "KBPermissionViewController.h"
#import <AVFoundation/AVFoundation.h>
static void *KBPermPlayerPresentationSizeContext = &KBPermPlayerPresentationSizeContext;
@interface KBPermissionViewController ()
@property (nonatomic, strong) UILabel *titleLabel; //
@property (nonatomic, strong) UILabel *tipsLabel; //
@@ -23,6 +25,9 @@
@property (nonatomic, strong) AVPlayer *kb_permPlayer;
@property (nonatomic, strong) AVPlayerLayer *kb_permPlayerLayer;
// KBFit(550)
@property (nonatomic, strong) UIView *videoContainerView;
@end
@implementation KBPermissionViewController
@@ -36,7 +41,7 @@
[self.view addSubview:self.closeButton];
[self.view addSubview:self.titleLabel];
[self.view addSubview:self.tipsLabel];
// [self.view addSubview:self.videoContainerView];
[self.view addSubview:self.videoContainerView];
[self.view addSubview:self.openButton];
[self.view addSubview:self.helpLabel];
@@ -70,13 +75,13 @@
make.left.equalTo(self.titleLabel);
}];
// view 16 tipsLabel 30 36
// [self.videoContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.top.equalTo(self.tipsLabel.mas_bottom).offset(30);
// make.left.equalTo(self.view).offset(16);
// make.right.equalTo(self.view).offset(-16);
// make.bottom.equalTo(self.openButton.mas_top).offset(-36);
// }];
// 25 tipsLabel 32 KBFit(550)
[self.videoContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.tipsLabel.mas_bottom).offset(32);
make.left.equalTo(self.view).offset(25);
make.right.equalTo(self.view).offset(-25);
make.height.mas_equalTo(KBFit(550));
}];
[self.openButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.view).offset(-KB_SAFE_BOTTOM-20);
@@ -107,13 +112,32 @@
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if (!self.kb_permPlayerLayer) { return; }
// videoGravity
self.kb_permPlayerLayer.frame = CGRectMake(25, CGRectGetMaxY(self.tipsLabel.frame) + 32, KB_SCREEN_WIDTH - 50, 700);
// videoContainerView
// KBFit(550)
CGSize videoSize = self.kb_permPlayer.currentItem.presentationSize;
if (videoSize.width <= 0) { return; } //
CGFloat aspect = videoSize.height / videoSize.width;
CGRect containerBounds = self.videoContainerView.bounds;
if (CGRectIsEmpty(containerBounds)) { return; }
CGFloat width = CGRectGetWidth(containerBounds); //
CGFloat height = width * aspect; //
// y = 0 height >
self.kb_permPlayerLayer.frame = CGRectMake(0, 0, width, height);
}
- (void)dealloc {
//
[[NSNotificationCenter defaultCenter] removeObserver:self];
@try {
[self.kb_permPlayer.currentItem removeObserver:self
forKeyPath:@"presentationSize"
context:KBPermPlayerPresentationSizeContext];
} @catch (__unused NSException *e) {
// ignore if observer not set
}
}
#pragma mark - Actions
@@ -180,13 +204,18 @@
self.kb_permPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.kb_permPlayer];
// 使 frame
self.kb_permPlayerLayer.videoGravity = AVLayerVideoGravityResize;
self.kb_permPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
self.kb_permPlayerLayer.cornerRadius = 20;
self.kb_permPlayerLayer.masksToBounds = true;
//
CALayer *buttonLayer = self.openButton.layer;
[self.view.layer insertSublayer:self.kb_permPlayerLayer below:buttonLayer];
//
[self.videoContainerView.layer addSublayer:self.kb_permPlayerLayer];
// presentationSize {0,0}
[item addObserver:self
forKeyPath:@"presentationSize"
options:NSKeyValueObservingOptionNew
context:KBPermPlayerPresentationSizeContext];
//
[[NSNotificationCenter defaultCenter] addObserver:self
@@ -208,6 +237,20 @@
}];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary<NSKeyValueChangeKey,id> *)change
context:(void *)context {
if (context == KBPermPlayerPresentationSizeContext) {
// presentationSize {0,0}
dispatch_async(dispatch_get_main_queue(), ^{
[self.view setNeedsLayout];
});
return;
}
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
#pragma mark - Lazy Subviews
- (UIButton *)backButton {
@@ -243,7 +286,6 @@
return _tipsLabel;
}
- (UIButton *)openButton {
if (!_openButton) {
_openButton = [UIButton buttonWithType:UIButtonTypeCustom];
@@ -286,6 +328,7 @@
}
return _bgImageView;
}
- (UIImageView *)redImageView{
if (!_redImageView) {
_redImageView = [[UIImageView alloc] init];
@@ -294,4 +337,14 @@
return _redImageView;
}
- (UIView *)videoContainerView {
if (!_videoContainerView) {
_videoContainerView = [UIView new];
_videoContainerView.backgroundColor = [UIColor clearColor];
_videoContainerView.layer.cornerRadius = 20.0;
_videoContainerView.clipsToBounds = YES; //
}
return _videoContainerView;
}
@end