MpMovieplayerController 点击手势识别器在全屏时不会触发
Posted
技术标签:
【中文标题】MpMovieplayerController 点击手势识别器在全屏时不会触发【英文标题】:MpMovieplayerController tap gesture recognizer doesn't trigger when in fullscreen 【发布时间】:2013-03-02 20:26:45 【问题描述】:我正在尝试使用UITapGestureRecognizer
来处理我的全屏视频上的点击。如果我省略 [self.player setFullscreen:YES animated:NO];
它可以工作,但我的视频将无法缩放以适应屏幕。
来自我的 .m:
- (void)viewDidLoad
[super viewDidLoad];
NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mov"];
player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:videoPath]];
player.shouldAutoplay = NO;
player.view.frame = self.view.bounds;
player.scalingMode = MPMovieScalingModeAspectFit;
player.controlStyle = MPMovieControlStyleNone;
player.fullscreen = YES;
self.player = player;
[self.player prepareToPlay];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
UIView *aView = [[UIView alloc] initWithFrame:player.view.bounds];
[aView addGestureRecognizer:tapGesture];
[self.player.view addSubview:aView];
- (IBAction)playMovie:(id)sender
//add the MPMoviePlayerViewController to this view (as subview)
//Play movie
[self.view addSubview:self.player.view];
[self.player setFullscreen:YES animated:NO]; //commenting out this will make it work
[self.player play];
- (void)handleTap:(UITapGestureRecognizer *)recognizer
NSLog(@"tap tap");
来自我的 .h:
@property (retain, nonatomic) MPMoviePlayerController *player;
- (void)handleTap:(UITapGestureRecognizer *)recognizer;
【问题讨论】:
当以你的方式使用全屏时,`MPMoviePlayerController 并没有真正使用它的正常视图,而是直接在窗口上播放。为了让您的代码也能在全屏模式下运行,一旦玩家切换到全屏模式,您就必须添加该手势识别器。您可能希望在窗口的视图堆栈中找到 MPMovieView 的实例并将其添加到该视图中。 【参考方案1】:你可以试试这个:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(willEnterFullScreen:)
name:MPMoviePlayerWillEnterFullscreenNotification
object:nil];
- (void)willEnterFullScreen:(NSNotification*)notification
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
UIView *aView = [[UIView alloc] initWithFrame:self.player.backgroundView.bounds];
[aView addGestureRecognizer:tapGesture];
[self.view.window addSubview:aView];
然后在发布 MPMoviePlayerWillExitFullscreenNotification 时删除您的子视图
【讨论】:
这看起来也很有希望,明天我也试试这个方法,看看它是如何工作的。谢谢你的建议。【参考方案2】:在我的评论中,我草拟了如何在使用适当的全屏 ([self.player setFullscreen:YES animated:NO];
) 时得到覆盖。
我建议您通过相应地设置其框架来简单地调整播放器视图的大小以覆盖整个屏幕。
您必须在初始化代码时去掉那个player.fullscreen = YES;
,但我想这已经很明显了。
【讨论】:
感谢您的建议,这似乎是解决我问题的简单方法。我只需要计算 scaleFactor(按宽度)以及新的 originY - 并将它们放在 CGRectMake() 函数中 - 效果很好!以上是关于MpMovieplayerController 点击手势识别器在全屏时不会触发的主要内容,如果未能解决你的问题,请参考以下文章
将 MPMoviePlayerController 视图置于 UIview 的中心(相同位置)
MPMoviePlayerViewController和MPMoviePlayerController的使用
MPMoviePlayerController 不播放 .wmv
如何显示 MPMoviePlayerController 控件?