【问题标题】:MPMoviePlayerController's view does not recognize touchMPMoviePlayerController 视图无法识别触摸
【发布时间】:2011-11-21 04:17:16
【问题描述】:

这是我的代码:

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


【问题讨论】:

  • 你为什么将它添加到 backgroundView 而不是视图?
  • 我用viewbackgroundView 尝试了这两种情况。 backgroundView 是最新的案例,现在我再次将其更改为查看。

标签: ios ipad mpmovieplayercontroller uigesturerecognizer


【解决方案1】:

其实这个问题的答案很简单:

  • 将自己设置为 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;
}

【讨论】:

  • 是的。通常,手势识别器假定它应该尽可能减少损坏。但是在极少数情况下,您希望将自定义触摸识别器添加到具有自己的识别器的视图中,您可以设置一个委托,以便在识别器应该协同工作时执行解析。这样的委托可以决定添加到视图中的每个识别器。这是一种非常有用的模式,可以减轻以前利用侵入视图内部的方法带来的许多痛苦。
  • 简单的 B R I L L I A N T !!!我不明白为什么您的回答不被接受。
  • 这似乎对我不起作用。我为仍然卡住的人添加了不同的解决方案
  • 效果很好。我的信念是,这与 MPMoviePlayerController 很可能添加自己的点击识别器(例如显示/隐藏播放器控件)有关,因此这种委托模式允许两者同时工作
  • @EgeAkpinar 确切地说,MPMoviePlayerController 及其视图使用多个手势识别器
【解决方案2】:

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

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

例如

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

这会得到你的点击,但你打破了控制。可能仍然有办法让它与其他手势交互。

【讨论】:

  • 刚刚意识到您已经将 controlStyle 设置为 NONE,所以我想这应该适合您!
【解决方案3】:

只是为了分享我在不添加自定义视图的情况下将自定义 UISwipeGestureRecognizer 添加到播放器视图的不同方法:

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

这是为了替代常规的调用方式

[player.view addGestureRecognizer:swipeGesture];

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

【讨论】:

  • [[anyview.subviews objectAtIndex:0] addGestureRecognizer:swipeGesture];像冠军一样工作......兄弟。干得好。
  • 诀窍是您提示添加swipeGesture!所以它保留了点击手势以保持播放器控件可用!太棒了! (Y)
【解决方案4】:

您可以为此使用 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
{

}

其中startPosition 是在.h 文件中声明的CGPoint

【讨论】:

    【解决方案5】:

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

    【讨论】:

      【解决方案6】:

      使用 MPMoviePlayerController 的最新变体,可接受的解决方案仍然对我不起作用。

      我还尝试“破解 UIView 堆栈”,并用​​手势识别器覆盖我自己的 UIView。但这一切都没有奏效。

      我终于实现了这个解决方案:'Overriding UIAplication',这似乎是一种过分的方法,但无论如何它都有效。只需在检测到触摸事件时为其添加某种标志或委托,然后您就可以停止电影。

      【讨论】:

        【解决方案7】:

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

        我所做的是在电影播放器​​视图上方添加UIView。这将阻止所有手势传递给玩家,但这是重点,因为我不希望任何控件可见。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-02-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多