MPMoviePlayerController 添加 UIButton 以查看淡入淡出的控件 [英] MPMoviePlayerController adding UIButton to view that fades with controls

查看:23
本文介绍了MPMoviePlayerController 添加 UIButton 以查看淡入淡出的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 UIButton 与标准控件一起添加到 MPMoviePlayerController 的视图中.该按钮出现在视频上并按预期工作,接收触摸事件,但我希望它使用标准控件淡入和淡出以响应用户触摸.

I am trying to add a UIButton to the view of a MPMoviePlayerController along with the standard controls. The button appears over the video and works as expected receiving touch events, but I would like to have it fade in and out with the standard controls in response to user touches.

我知道我可以通过滚动我自己的自定义播放器控件来实现这一点,但这似乎很愚蠢,因为我只是想添加一个按钮.

I know I could accomplish this by rolling my own custom player controls, but it seems silly since I am just trying to add one button.

编辑

如果您递归地遍历MPMoviePlayerController 的视图的视图层次结构,最终您将来到一个名为MPInlineVideoOverlay 的视图类.您可以轻松地向此视图添加任何其他控件,以实现自动淡入/淡出行为.

If you recursively traverse the view hierarchy of the MPMoviePlayerController's view eventually you will come to a view class called MPInlineVideoOverlay. You can add any additional controls easily to this view to achieve the auto fade in/out behavior.

虽然有一些问题,但在您创建 MPMoviePlayerController 并将其添加到视图之后,有时可能需要一段时间(根据我的经验),然后它才能完全初始化并创建它是 MPInlineVideoOverlay 层.因此,我必须在下面的代码中创建一个名为 controlView 的实例变量,因为有时在此代码运行时它不存在.这就是为什么我有最后一段代码,如果找不到,函数会在 0.1 秒内再次调用自身.尽管有此延迟,但我没有注意到界面上显示的按钮有任何延迟.

There are a few gotchas though, it can sometimes take awhile (up to a second in my experience) after you have created the MPMoviePlayerController and added it to a view before it has initialized fully and created it's MPInlineVideoOverlay layer. Because of this I had to create an instance variable called controlView in the code below because sometimes it doesn't exist when this code runs. This is why I have the last bit of code where the function calls itself again in 0.1 seconds if it isn't found. I couldn't notice any delay in the button appearing on my interface despite this delay.

-(void)setupAdditionalControls {
    //Call after you have initialized your MPMoviePlayerController (probably viewDidLoad)
    controlView = nil;
    [self recursiveViewTraversal:movie.view counter:0];

    //check to see if we found it, if we didn't we need to do it again in 0.1 seconds
    if(controlView) {
            UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [controlView addSubview:backButton];
    } else {
            [self performSelector:@selector(setupAdditionalControls) withObject:nil afterDelay:0.1];
    }
}


-(void)recursiveViewTraversal:(UIView*)view counter:(int)counter {
    NSLog(@"Depth %d - %@", counter, view); //For debug
    if([view isKindOfClass:NSClassFromString(@"MPInlineVideoOverlay")]) {
            //Add any additional controls you want to have fade with the standard controls here
            controlView = view;
    } else {
            for(UIView *child in [view subviews]) {
                    [self recursiveViewTraversal:child counter:counter+1];
            }
    }
}

这不是最好的解决方案,但我发布它以防其他人尝试做同样的事情.如果 Apple 要更改控件覆盖层内部的视图结构或类名称,它就会中断.我还假设您没有全屏播放视频(尽管您可以使用嵌入式控件全屏播放).我还必须使用此处描述的技术禁用全屏按钮,因为 MPInlineVideoOverlay 视图在按下时会被删除并释放:iPad MPMoviePlayerController - 禁用全屏

It isn't the best solution, but I am posting it in case someone else is trying to do the same thing. If Apple was to change the view structure or class names internal to the control overlay it would break. I am also assuming you aren't playing the video full screen (although you can play it fullscreen with embeded controls). I also had to disable the fullscreen button using the technique described here because the MPInlineVideoOverlay view gets removed and released when it is pressed: iPad MPMoviePlayerController - Disable Fullscreen

当您收到上述全屏通知时调用 setupAdditionalControls 会将您的附加控件重新添加到 UI.

Calling setupAdditionalControls when you receive the fullscreen notifications described above will re-add your additional controls to the UI.

如果有人能提出我想出的这个黑客以外的建议,我会喜欢一个更优雅的解决方案.

Would love a more elegant solution if anyone can suggest something other than this hackery I have come up with.

推荐答案

我对同样问题的解决方案是:

My solution to the same problem was:

  • 将按钮添加为 MPMoviePlayerController 视图的子项;
  • 使用其 alpha 属性的动画淡入和淡出按钮,并具有适当的持续时间;
  • 处理播放器控制器的 touchesBegan,并使用它来切换按钮的可见性(使用其 alpha);
  • 使用计时器来确定何时再次隐藏按钮.
  • Add the button as a child of the MPMoviePlayerController's view;
  • fade the button in and out using animation of its alpha property, with the proper durations;
  • handle the player controller's touchesBegan, and use that to toggle the button's visibility (using its alpha);
  • use a timer to determine when to hide the button again.

通过反复试验,我确定与(当前)iOS 匹配的持续时间是:

By trial-and-error, I determined that the durations that matched the (current) iOS ones are:

  • 淡入:0.1s
  • 淡出:0.2s
  • 屏幕持续时间:5.0s(每次触摸视图时延长)

当然,这仍然是脆弱的;如果内置延迟发生变化,我的会看起来不对,但代码仍然会运行.

Of course this is still fragile; if the built-in delays change, mine will look wrong, but the code will still run.

这篇关于MPMoviePlayerController 添加 UIButton 以查看淡入淡出的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆