MPMoviePlayerController 添加 UIButton 以随控件淡出的视图

发布于 2024-11-09 07:42:27 字数 2150 浏览 0 评论 0原文

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

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

编辑

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

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

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

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

通话setupAdditionalControls 当您收到上述全屏通知时,会将您的附加控件重新添加到 UI。

如果有人能提出除了我提出的这个黑客之外的其他建议,我会喜欢一个更优雅的解决方案。

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

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.

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失与倦" 2024-11-16 07:42:27

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

  • 将按钮添加为 MPMoviePlayerController 视图的子级;
  • 使用其 alpha 属性的动画以适当的持续时间淡入和淡出按钮;
  • 处理播放器控制器的 touchesBegan,并使用它来切换按钮的可见性(使用其 alpha);
  • 使用计时器来确定何时再次隐藏按钮。

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

  • 淡入:0.1 秒
  • 淡出:0.2 秒
  • 屏幕上的持续时间:5.0 秒(每次触摸视图时都会延长

)这仍然是脆弱的;如果内置延迟发生变化,我的延迟会看起来错误,但代码仍然会运行。

My solution to the same problem was:

  • 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.

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

  • fade in: 0.1s
  • fade out: 0.2s
  • duration on screen: 5.0s (extend that each time the view is touched)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文