自定义按钮在 AVPlayer iOS 11 中不起作用,但它在 iOS 11 以下可以正常工作?
Custom button not working in AVPlayer iOS 11 but it's perfectly working below iOS 11?
我知道这可能是重复的问题,但已经存在的问题没有解决方案,所以我再次要求可能开始赏金问题。
这是我的代码,它在 iOS 11 :
下面运行
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
// create a player view controller
avcontroller = [[AVPlayerViewController alloc]init];
avcontroller.player = player;
[player play];
NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter];
[noteCenter addObserverForName:AVPlayerItemDidPlayToEndTimeNotification
object:nil
queue:nil
usingBlock:^(NSNotification *note) {
NSLog(@"Video end");
self.avcontroller.player = nil;
[self.avcontroller.view removeFromSuperview];
[self.view addSubview:delegate.tabViewControllerObj.tabBarController.view];
}];
_skip =[UIButton buttonWithType:UIButtonTypeCustom];
[_skip addTarget:self action:@selector(btnSkipTapped)
forControlEvents:UIControlEventTouchUpInside];
[_skip setTitle:@"Skip" forState:UIControlStateNormal];
_skip.layer.borderWidth = 2.0f;
_skip.layer.cornerRadius = 12;
_skip.clipsToBounds = YES;
_skip.layer.borderColor = [UIColor whiteColor].CGColor;
// show the view controller
[self addChildViewController:avcontroller];
[avcontroller.view addSubview:_skip];
[avcontroller.view bringSubviewToFront:_skip];
[self.view addSubview:avcontroller.view];
avcontroller.view.frame = self.view.frame;
}
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
_skip.frame = CGRectMake((self.view.frame.size.width-140),(self.view.frame.size.height-150),100.0,35.0);
}
这里是点击按钮的功能:
- (IBAction)btnSkipTapped{
NSLog(@"Skip button clicked");
self.avcontroller.player = nil;
[self.avcontroller.view removeFromSuperview];
[self.view addSubview:delegate.tabViewControllerObj.tabBarController.view];
}
此代码将完美显示按钮,但在点击按钮时,它没有调用 btnSkipTapped 函数。
任何帮助将不胜感激!
@DeepShah 您在 avcontroller.view
中添加了跳过按钮,因此它可以正常显示,但您无法点击它,因为 AVPlayerViewControllerContentView
覆盖了该跳过按钮。
见下图:
因此您必须在 self.view
及其作品中添加跳过按钮。
[self.view addSubview:_skip];
[self.view bringSubviewToFront:_skip];
我知道这可能是重复的问题,但已经存在的问题没有解决方案,所以我再次要求可能开始赏金问题。
这是我的代码,它在 iOS 11 :
下面运行AVPlayer *player = [AVPlayer playerWithURL:videoURL];
// create a player view controller
avcontroller = [[AVPlayerViewController alloc]init];
avcontroller.player = player;
[player play];
NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter];
[noteCenter addObserverForName:AVPlayerItemDidPlayToEndTimeNotification
object:nil
queue:nil
usingBlock:^(NSNotification *note) {
NSLog(@"Video end");
self.avcontroller.player = nil;
[self.avcontroller.view removeFromSuperview];
[self.view addSubview:delegate.tabViewControllerObj.tabBarController.view];
}];
_skip =[UIButton buttonWithType:UIButtonTypeCustom];
[_skip addTarget:self action:@selector(btnSkipTapped)
forControlEvents:UIControlEventTouchUpInside];
[_skip setTitle:@"Skip" forState:UIControlStateNormal];
_skip.layer.borderWidth = 2.0f;
_skip.layer.cornerRadius = 12;
_skip.clipsToBounds = YES;
_skip.layer.borderColor = [UIColor whiteColor].CGColor;
// show the view controller
[self addChildViewController:avcontroller];
[avcontroller.view addSubview:_skip];
[avcontroller.view bringSubviewToFront:_skip];
[self.view addSubview:avcontroller.view];
avcontroller.view.frame = self.view.frame;
}
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
_skip.frame = CGRectMake((self.view.frame.size.width-140),(self.view.frame.size.height-150),100.0,35.0);
}
这里是点击按钮的功能:
- (IBAction)btnSkipTapped{
NSLog(@"Skip button clicked");
self.avcontroller.player = nil;
[self.avcontroller.view removeFromSuperview];
[self.view addSubview:delegate.tabViewControllerObj.tabBarController.view];
}
此代码将完美显示按钮,但在点击按钮时,它没有调用 btnSkipTapped 函数。 任何帮助将不胜感激!
@DeepShah 您在 avcontroller.view
中添加了跳过按钮,因此它可以正常显示,但您无法点击它,因为 AVPlayerViewControllerContentView
覆盖了该跳过按钮。
见下图:
因此您必须在 self.view
及其作品中添加跳过按钮。
[self.view addSubview:_skip];
[self.view bringSubviewToFront:_skip];