MPMoviePlayerController添加UIButton以查看使用控件淡化 [英] MPMoviePlayerController adding UIButton to view that fades with controls

查看:161
本文介绍了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.

编辑

EDIT

如果以递归方式遍历 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 上面描述的全屏通知会将您的其他控件重新添加到用户界面。

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

如果有人可以提出除了我提出的这个hackery之外的其他内容,那么会喜欢更优雅的解决方案吗? 。

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 视图的子项;

  • 使用其<$ c的动画淡入淡出按钮$ c> alpha 属性,具有适当的持续时间;

  • 处理播放器控制器s 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天全站免登陆