MPMoviePlayerController的视图不能识别触摸
这是我的代码:
_mediaPlayer = [[MPMoviePlayerController alloc] init]; _mediaPlayer.controlStyle = MPMovieControlStyleNone; _mediaPlayer.shouldAutoplay = NO; [_mediaPlayer.view setFrame: CGRectMake(5, 5, 600,400)]; [playerHolder addSubview: _mediaPlayer.view]; // [self prepareScreenContentToPlay]; // UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleRollTap:)]; singleFingerTap.numberOfTapsRequired = 1; [_mediaPlayer.view addGestureRecognizer:singleFingerTap]; [singleFingerTap release];
和手势识别器的动作方法:
-(void)handleRollTap:(UITapGestureRecognizer*)sender{ NSLog(@"%@", @"touch"); }
MPMoviePlayerController工作正常。 另外我想处理MPMoviePlayerController视图上的触摸,但handleRollTap
从不调用。 为什么MPMoviePlayerController的视图不适用于UITapGestureRecognizer?
好。 如果singleFingerTap.numberOfTapsRequired = 2;
那么所有的工作也很好。 但没有一个单一的水龙头..
其实答案很简单:
- 将自己设置为UIGestureRecognizer委托
- 返回YES代表方法:
例如
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture)]; tapGestureRecognizer.delegate = self;
和代码中的其他地方:
#pragma mark - gesture delegate // this allows you to dispatch touches - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { return YES; } // this enables you to handle multiple recognizers on single view - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; }
MPMoviePlayerController有一个子视图,它占据了整个边界,而且子视图上有3个手势识别器(在iOS 4.3中)。
mp = [[MPMoviePlayerController alloc] initWithURL:movieURL]; mp.frame = aRectangle; for (UIGestureRecognizer *g in ((UIView *)[mp.view.subviews objectAtIndex:0]).gestureRecognizers) { NSLog(@"g %@", g); }
会输出:
g <MPTapGestureRecognizer: 0x6224c30; baseClass = UIGestureRecognizer; state = Possible; cancelsTouchesInView = NO; view = <MPSwipableView 0x6416100>; target= <(action=_tapGestureRecognized:, target=<MPSwipableView 0x6416100>)>> g <UIPinchGestureRecognizer: 0x6224710; state = Possible; cancelsTouchesInView = NO; delaysTouchesEnded = NO; view = <MPSwipableView 0x6416100>; target= <(action=_pinchGestureRecognized:, target=<MPSwipableView 0x6416100>)>> g <MPActivityGestureRecognizer: 0x6224640; baseClass = UIGestureRecognizer; state = Possible; cancelsTouchesInView = NO; delaysTouchesEnded = NO; view = <MPSwipableView 0x6416100>; target= <(action=_activityGestureRecognized:, target=<MPSwipableView 0x6416100>)>>
所以已经有一个GestureRecognizer处理一个水龙头,但它不是一个UITapGestureRecognizer,而是一个MPTapGestureRecognizer(一个电影播放器的自定义识别器)。
如果您创build一个通用视图并将其添加到电影播放器视图层次结构中,则可以添加触摸,但会阻止电影播放器的触摸(因此,单击不会使控件消失)。
例如
UIView *aView = [[UIView alloc] initWithFrame:mp.view.bounds]; [aView addGestureRecognizer:tapGesture]; [mp.view addSubview:aView];
这会让你的水龙头,但你打破了控制。 可能还有一种方法可以让它与其他手势互动。
只是为了分享我不同的方法来添加一个自定义的UISwipeGestureRecognizer到播放器视图而不添加一个自定义视图:
[[player.view.subviews objectAtIndex:0] addGestureRecognizer:swipeGesture];
这是取代传统的调用方法
[player.view addGestureRecognizer:swipeGesture];
因为它只能在iPad非全屏模式和iPhone上使用。 当玩家在iPad上进入全屏模式时,手势不起作用
你可以使用UIView
UITouch
事件。 以一个UIView
和MPMoviePlayerController
里面:
theMovie = [MPMoviePlayerController new]; theMovie.view.frame = CGRectMake(0, 0, 1024, 768); [theMovie setContentURL:theURL]; [theMovie setScalingMode:MPMovieScalingModeAspectFit]; [theMovie setCurrentPlaybackTime:0.2]; [theMovie setFullscreen:YES animated:YES]; [self addSubview:theMovie.view]; [viewMovie addSubview:theMovie.view];
使用UIView
touch委托方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"touchesBegan"); UITouch *touch = [touches anyObject]; startPosition = [touch locationInView:self]; [viewMovie touchesBegan:touches withEvent:event]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"touchesMoved"); UITouch *touch = [touches anyObject]; CGPoint endPosition = [touch locationInView:self]; if (startPosition.x < endPosition.x) { NSLog(@"Left to Right"); } else { NSLog(@"Right to Left"); } } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { }
startPosition
是在.h
文件中声明的CGPoint
。
根本原因是应在MPVideoBackgroundView上添加新的手势识别器,即MPMoviePlayerController视图的子视图。
随着MPMoviePlayerController的最新变种,接受的解决scheme仍然没有为我工作。
我也尝试过“黑客UIView堆栈”,并用手势识别器覆盖我自己的UIView。 但这一切都没有奏效。
我终于实现了这个解决scheme:' 覆盖UIAplication ',这似乎是一个顶级的方式来做到这一点,但在任何情况下,它的工作原理。 只要在检测到触摸事件时添加某种标记或代表,就可以停止播放电影了。
devdavid有正确的想法,但他的解决scheme并不适合我。 我猜想在更新的播放器版本中,电影播放器视图上方还有其他视图。
我所做的是在电影播放器视图上添加一个UIView
。 这将阻止所有的手势进入玩家,但这是整个点,因为我不希望任何控制可见。