我有一个电影播放器视图控制器,我想在其上添加叠加视图。
我使用了以下代码:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
[[moviePlayer view] addSubview:[self overlayControlsView]];
UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[[self view] addGestureRecognizer:tap];
}
return self;
}
overlayControlsView目前只是一个UILabel。
- (void)handleTap:(UITapGestureRecognizer *)sender {
NSLog(@"Tap handled");
if ([sender state] == UIGestureRecognizerStateEnded) {
if ([[self overlayControlsView] isHidden]) {
[[self overlayControlsView] setHidden:NO];
} else {
[[self overlayControlsView] setHidden:YES];
}
}
}
当电影播放器刚刚显示时,点击它会导致overlayControlsView显示并成功隐藏。
然而,在moviewplayer加载状态改变并开始播放之后(我从互联网加载视频,因此这里有一些时间间隔),不再调用handleTap方法。
有人对此有任何想法吗?有没有更好的方法来添加叠加控件?
答案 0 :(得分:1)
我不知道你的代码有什么问题;这是使用自定义按钮
完成叠加控制的替代方法- (void)viewDidLoad {
[super viewDidLoad];
NSString *urlString = [[NSBundle mainBundle] pathForResource:@"sample-video" ofType:@"mov"];
player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:urlString]];
UIButton *overlayView = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayView setTitle:@"" forState:UIControlStateNormal];
[overlayView setTitle:@"Play" forState:UIControlStateSelected];
[overlayView addTarget:self action:@selector(handleTap:) forControlEvents:UIControlEventTouchUpInside];
player.view.frame = CGRectMake(50, 50, 200, 250);
overlayView.frame = player.view.frame;
[player.view addSubview:overlayView];
player.controlStyle = MPMovieControlStyleNone;
[self.view addSubview:player.view];
}
- (void)handleTap:(id *)sender {
UIButton *btn = (UIButton *)sender;
if (btn.selected) {
[player play];
[btn setSelected:NO];
}
else {
[player pause];
[btn setSelected:YES];
} }