MPMoviePlayerController的视图无法识别触摸

时间:2011-09-20 14:49:05

标签: ios ipad mpmovieplayercontroller uigesturerecognizer

这是我的代码:

_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;那么一切正常。但没有什么可以单击..


7 个答案:

答案 0 :(得分:63)

实际上,答案很简单:

  • 将自己设置为UIGestureRecognizer委托
  • 为委托方法返回YES:

e.g。

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;
}

答案 1 :(得分:21)

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(电影播放器​​的自定义识别器)。

如果您创建一个通用视图并将其添加到电影播放器​​视图层次结构中,您可以添加触摸,但它会阻止对电影播放器​​的触摸(因此单击一下就不会使控件消失)。 / p>

e.g。

UIView *aView = [[UIView alloc] initWithFrame:mp.view.bounds];
[aView addGestureRecognizer:tapGesture];
[mp.view addSubview:aView];

这会让你点击,但你打破了控制。可能仍有一种方法可以让它与其他手势进行交互。

答案 2 :(得分:8)

只是为了分享我在播放器视图中添加自定义UISwipeGestureRecognizer的不同方法而不添加自定义视图:

[[player.view.subviews objectAtIndex:0] addGestureRecognizer:swipeGesture];

这是为了取代传统的调用方法

[player.view addGestureRecognizer:swipeGesture];

因为它仅适用于iPad非全屏模式和iPhone。当播放器在iPad上进入全屏模式时,手势无效

答案 3 :(得分:1)

您可以使用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触摸委托方法

- (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
{

}

其中startPositionCGPoint文件中声明的.h

答案 4 :(得分:0)

根本原因是应该在MPVideoBackgroundView上添加新的手势识别器,这是MPMoviePlayerController视图的子视图。

答案 5 :(得分:0)

使用MPMoviePlayerController的最新版本,接受的解决方案仍然不适用于我。

我也尝试'破解UIView堆栈',并用手势识别器覆盖我自己的UIView。但这一切都行不通。

我终于实现了这个解决方案:'Overriding UIAplication',这似乎是最重要的方法,但无论如何它都有效。只需在检测到触摸事件时添加某种标记或委托,然后就可以停止播放。

答案 6 :(得分:0)

devdavid有正确的想法,但他的解决方案对我不起作用。我想在播放器的新版本中,电影播放器​​视图上方还有其他视图。

我所做的是在电影播放器​​视图上方添加UIView。这样可以防止所有手势进入播放器,但这就是重点,因为我不希望任何控件可见。