关于MPMoviePlayerController禁用缩放和点击

今天再次捣鼓MPMoviePlayerController,为了整出来如下图的简洁效果,并禁用右边的“全屏”按钮查到的资料,记录一下。
iOS Simulator Screen shot 2013-2-18 PM10.42.46
禁用“全屏”按钮。
不禁用它的话,一点击就又切换到了默认的控制按钮的状态,不是我们想要的,方法很取巧,就是发现全屏的通知的时候,马上给他改回来:
- (void)viewDidLoad {
[super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(movieEventFullscreenHandler:) 
                                                 name:MPMoviePlayerWillEnterFullscreenNotification 
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(movieEventFullscreenHandler:) 
                                                 name:MPMoviePlayerDidEnterFullscreenNotification 
                                               object:nil];

    self.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
}

- (void)movieEventFullscreenHandler:(NSNotification*)notification {
    [self.moviePlayer setFullscreen:NO animated:NO];
    [self.moviePlayer setControlStyle:MPMovieControlStyleEmbedded];
}

这么干的一个很酷的地方就是顺带把二指捏合张开的手势也给禁用了。
上面的方法是最后用到的,开始的思路是,监听控制按钮出现时,逐层查找到那个“全屏”按钮,然后给拿掉或者给覆盖了。结果试了半天逐层根本找不到,覆盖也盖不上,真牛x,倒是学到了下面的方法
1.代码显示隐藏控制按钮
- (void)showControls
{
for(id views in [[player view] subviews]){
for(id subViews in [views subviews]){
for (id controlView in [subViews subviews]){
if ( [controlView isKindOfClass:NSClassFromString(@"MPInlineVideoOverlay")] ) {
[controlView setAlpha:1.0];
[controlView setHidden:NO];
}
}
}
}
}

  • (void)hideControls
    {
    for(id views in [[player view] subviews]){
    for(id subViews in [views subviews]){
    for (id controlView in [subViews subviews]){
    if ( [controlView isKindOfClass:NSClassFromString(@"MPInlineVideoOverlay")] ) {
    [controlView setAlpha:0.0];
    [controlView setHidden:YES];
    }
    }
    }
    }
    }

    2.判断控制按钮的状态
    BOOL controlsVisible = NO;
    for(id views in [[_moviePlayer view] subviews]){
    for(id subViews in [views subviews]){
    for (id controlView in [subViews subviews]){
    controlsVisible = ([controlView alpha] <= 0.0) ? (NO) : (YES);
    }
    }
    }
    NSLog(@"player controls are visible: %d", controlsVisible);

    3.截取显示隐藏控制按钮的点击事件
    其实就是把下面的方法添加到MPVideoBackgroundView这一层上
    -(void) addTargetActionToGesture:(UIView )view
    {
    NSArray
    gestureGroup = [view gestureRecognizers];
    for(UITapGestureRecognizer * g in gestureGroup)
    {
    [g addTarget:selfaction:@selector(handleMoviePressed:)];
    }
    }

    MPMoviePlayerController的字视图层级
    (lldb) po [m_view recursiveDescription]
    (id) $1 = 0x179529f0 <UIView: 0xfdb55f0; frame = (0 0; 604 256); layer = <CALayer: 0xfd83a10>>
    | <MPMovieView: 0xde71d70; frame = (0 0; 604 256);
    | | <MPSwipableView: 0x1d774dc0; frame = (0 0; 604 256);
    | | | <MPVideoBackgroundView: 0x1d775160; frame = (0 0; 604 256);
    | | | | <UIView: 0x1d776160; frame = (0 0; 604 256);
    | | | | <MPVideoView: 0x1d7740e0; frame = (0 0; 604 256);
    | | | | | <UIView: 0x1d773fc0; frame = (142 -32; 320 320);
    | | | | | | <MPVideoBufferLayerContainer: 0x1d772050> (layer)
    | | | | | | | <MPVideoBufferLayer: 0x22ebdb20> (layer)
    | | | | | | | | <AVPlayerLayerContentLayer: 0xf99d780> (layer)
    | | | | | | | | | <FigVideoLayer: 0xdbaab80> (layer)
    | | | | | | | | <AVPlayerLayerSubtitleLayer: 0x22eaf480> (layer)
    | | | | | | | | | <AVSubtitleTextLayer: 0x22ea70f0> (layer)
    | | | | <MPInlineVideoOverlay: 0x1d77e7c0; frame = (0 0; 604 256);
    | | | | | <MPInlineTransportControls: 0x1d77edc0; frame = (0 212; 604 44);
    | | | | | | <MPDetailSlider: 0x1d7837c0; baseClass = UISlider; frame = (70 12; 475 24);
    | | | | | | | <UILabel: 0x1d78e1b0; frame = (-8 -2; 45 25); text = '0:00';
    | | | | | | | <UILabel: 0x1d78e240; frame = (438 -2; 45 25); text = '-0:00';
    | | | | | | | <UIImageView: 0x1d78e300; frame = (68 5; 360 11);
    | | | | | | | <UIImageView: 0x1d78e3a0; frame = (47 5; 21 11);
    | | | | | | | <UIImageView: 0x1d78c7e0; frame = (47 6; 381 10);
    | | | | | | | <UIImageView: 0x1d78e430; frame = (57 2; 22 21);
    | | | | | | <MPTransportButton: 0x1d785a50; baseClass = UIButton; frame = (34 14; 16 18);
    | | | | | | | <UIImageView: 0x1d785c70; frame = (0 0; 16 18);
    | | | | | | | <UIImageView: 0x1d785bb0; frame = (-42 -41; 100 100);
    | | | | | | <MPTransportButton: 0x1d785dd0; baseClass = UIButton; frame = (564 16; 13 14);
    | | | | | | | <UIImageView: 0x1d785ff0; frame = (0 0; 13 14);
    | | | | | | | <UIImageView: 0x1d785f30; frame = (-43.5 -43; 100 100);
    | | | | | | <UILabel: 0xde72ce0; frame = (265 13; 74 19); text = 'Loading…';
    | | | | | | <UIActivityIndicatorView: 0xde730d0; frame = (239 12; 20 20);
    | | | | | | | <UIImageView: 0xde77e90; frame = (0 0; 20 20);
    | | | | | <MPNowPlayingItemQueueInfoButton: 0x1d786690; baseClass = UIButton; frame = (251 10; 103 23);
    | | | | | | <UIImageView: 0x1d786750; frame = (0 0; 103 23);
    | | | | | | <UIButtonLabel: 0x1d788a50; frame = (0 0; 0 0);

参考资料:
iOS - disable two-finger zoom for MPMoviePlayerController fullscreen
How to detect when iPhone MPMoviePlayer controls appear/disappear?
iOS - Showing/Hiding MPMoviePlayerController Controls
拦截MPMoviePlayerController的点击事件

About rainbird

IOS攻城狮
This entry was posted in iOS, object-c and tagged , , , , , , . Bookmark the permalink.

One Response to 关于MPMoviePlayerController禁用缩放和点击

  1. yanw says:

    ios5下有效果 ios6下面木有效果

发表评论