在iOS中模拟YouTube Mini Player

时间:2015-07-23 03:01:49

标签: ios swift youtube

在YouTube应用中,我可以通过右下角的迷你播放器继续观看视频(请参阅下面标题为“The Chainsmokers”的视频),当我浏览所有其他视图控制器时应用程序。无论我在哪个页面,我都可以在迷你播放器中观看视频。

如果从迷你播放器向上拖动,则会显示视频的整个视图控制器。但它总是在其他页面上。我正在尝试了解如何使用Swift复制此方法,以便我可以从任何页面观看视频。

我的应用程序的结构方式是UIPageViewController。我嵌入了三个独特的导航控制器(用于滑动导航目的(见下面的代码))。我想了解如何利用迷你播放器。在YouTube中,是否像迷你模式VC?如何在结构上进行布局,如果您知道,我如何将其纳入我当前的布局?

class PageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

    private var pages: [UINavigationController]!

    private var currentPageIndex: Int!

    override func viewDidLoad() {
        super.viewDidLoad()
            self.dataSource = self
            self.delegate = self

            self.pages = [
                self.storyboard!.instantiateViewControllerWithIdentifier("Nav1") as! UINavigationController,
                self.storyboard!.instantiateViewControllerWithIdentifier("Nav2") as! UINavigationController,
                self.storyboard!.instantiateViewControllerWithIdentifier("Nav3") as! UINavigationController
            ]

            (self.pages[0].topViewController as! Nav1).parentPageViewController = self
            (self.pages[1].topViewController as! Nav2).parentPageViewController = self
            (self.pages[2].topViewController as! Nav3).parentPageViewController = self


            self.currentPageIndex = 1
            let startingViewController = self.pages[1] as UINavigationController
            self.setViewControllers([startingViewController], direction: .Forward, animated: false, completion: nil)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        let index = (self.pages as NSArray).indexOfObject(viewController)
        self.currentPageIndex = index

        // if currently displaying last view controller, return nil to indicate that there is no next view controller
        return (self.currentPageIndex == self.pages.count - 1 ? nil : self.pages[self.currentPageIndex + 1])
    }
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
        let index = (self.pages as NSArray).indexOfObject(viewController)
        self.currentPageIndex = index

        // if currently displaying first view controller, return nil to indicate that there is no previous view controller
        return (index == 0 ? nil : self.pages[index - 1])
    }
func displayPageForIndex(index: Int, animated: Bool = true) {
        assert(index >= 0 && index < self.pages.count, "Error: Attempting to display a page for an out of bounds index")

        // nop if index == self.currentPageIndex
        if self.currentPageIndex == index { return }

        if index < self.currentPageIndex {
            self.setViewControllers([self.pages[index]], direction: .Reverse, animated: true, completion: nil)
        } else if index > self.currentPageIndex {
            self.setViewControllers([self.pages[index]], direction: .Forward, animated: true, completion: nil)
        }

        self.currentPageIndex = index
    }

}

enter image description here

1 个答案:

答案 0 :(得分:9)

这很容易做到。您将子类化UIWindow,添加一个迷你播放器视图并确保它始终位于视图层次结构的顶部。首先在AppDelegate添加此代码以覆盖您的窗口:

/*!
 *  Subclassing UIWindow
 */
-(UIWindow *)window
{
    static YourWindow *customWindow = nil;
    if (!customWindow) customWindow = [[YourWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    return customWindow;
}

现在在YourWindow课程中,添加miniPlayer

-(void)awakeFromNib
{
    [self addMiniPlayer];
}

-(void)addMiniPlayer
{
    UIView *miniPlayer = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)]; //change to your frame here
    [self addSubview:miniPlayer];
    miniPlayer.backgroundColor = [UIColor redColor];
    self.miniPlayer = miniPlayer;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    [self bringSubviewToFront:self.miniPlayer];

    // You would like to update your miniplayer frame here.
    //CGRect frame = self.miniPlayer.frame;
    //frame.origin.x = self.frame.size.width - frame.size.width - 5;
    //frame.origin.y = 70;
    //self.appInfoLabel.frame = frame;
} 

现在,您需要调整miniPlayer的行为。您可以从AppDelegate访问它,如下所示:self.window.miniPlayer

更新:以下是Swift编写的示例:https://github.com/sahara108/TestCustomWindow