iOS-怒懟面試官:百行代碼高仿抖音(Tik Tok)

首先說一下:大家應(yīng)該都知道現(xiàn)在面試官太裝逼了。我認(rèn)識一位從360出來的人說:一個alloc你都要讓我轉(zhuǎn)成rutime.再成c函數(shù),然后??臻g,葉子函數(shù)再去問匯編,太屎了。

前陣子面試的時(shí)候也是如此,后來再問到一些蘋果官方提供的一些控件時(shí)卻居然一點(diǎn)都不知道。

比如有一個面試官問:一個業(yè)務(wù)上要求左右滑動時(shí)切換頁面,怎么做?

我的回答是:左右滑動的模塊有幾個就寫幾個基于UIViewController的類,然后這些模塊由一個UIPageViewController去控制。

我說了各種各樣的好處之后,面試官還不以為意,說我用一個UIScrollView去控制不行嗎?設(shè)置他的frame就行了。

我曾經(jīng)在這篇文章說過時(shí)至今日使用frame的缺點(diǎn)太突出了,我個人不建議。另一方面,如此多的代碼寫在一個VC當(dāng)中,那么業(yè)務(wù)代碼耦合的就太厲害了。而且我也在這篇文章里說過蘋果在UIKit里出來一個Container的概念。

之后面試官又問:如果我要是在之后的需求當(dāng)中再加上上下滑動呢?類似抖音那種。

我的回答便是:使用UIPageViewController更容易實(shí)現(xiàn)。效果如下:

代碼運(yùn)行之后的效果圖1

代碼運(yùn)行之后的效果圖2

鑒于Xcode自帶的"Debug view Hierarchy"不太好用,大家湊合著看這個效果。

抖音的效果是:視頻列表頁可以上下滑動以切換視頻源,同時(shí)視頻列表往右滑動則到搜索頁,往左側(cè)滑動則到up主的用戶詳情頁。

接下來我就說說我的思路(以Swift語言來實(shí)現(xiàn))。
第一步:創(chuàng)建一個新工程,默認(rèn)rootVC是工程中的ViewController

第二步:基于UIViewController分別創(chuàng)建三個VC,為SearchViewController(搜索頁),VideosViewController(視頻列表頁)和UserInfoViewController(up主的用戶信息頁)。如下圖:

第三步:在ViewController里創(chuàng)建UIPageViewController,并且實(shí)現(xiàn)切換三個模塊。代碼如下:

override func viewDidLoad() {
        super.viewDidLoad()
        
        // 創(chuàng)建搜索頁,視頻列表頁和用戶詳情頁
        let searchVC : SearchViewController = SearchViewController()
        let videosVC : VideosViewController = VideosViewController()
        let userInfoVC : UserInfoViewController = UserInfoViewController()
        self.viewControllers = [searchVC, videosVC, userInfoVC]
        
        // 創(chuàng)建UIPageViewController
        let pageVC : UIPageViewController = UIPageViewController.init(transitionStyle: UIPageViewController.TransitionStyle.scroll, navigationOrientation: UIPageViewController.NavigationOrientation.horizontal, options: nil)
        pageVC.setViewControllers([videosVC], direction: UIPageViewController.NavigationDirection.forward, animated: false, completion: nil)
        pageVC.dataSource = self
        pageVC.delegate = self
        
        // 將pageVC添加到當(dāng)前VC
        self.addChild(pageVC)
        self.view.addSubview(pageVC.view)
        pageVC.view.frame = self.view.bounds
        pageVC.didMove(toParent: self)
    }
    // UIPageViewController必須的兩個代理方法,這兩個代理方法控制三個頁面的先后順序
 func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        
        var index : Int = self.viewControllers.index(of: viewController) ?? NSNotFound
        if (index == 0) || (index == NSNotFound) {
            return nil
        }
        
        index -= 1
        
        return self.viewControllers[index]
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        var index : Int = self.viewControllers.index(of: viewController) ?? NSNotFound
        if (index == 0) || (index == NSNotFound) {
            return nil
        }
        
        index += 1
        if index >= self.viewControllers.count {
            return nil
        }
        
        return self.viewControllers[index]
    }

經(jīng)過以上三步就簡單的實(shí)現(xiàn)了三個頁面的左右切換。那么視頻列表的上下切換也是基于UIPageViewController的,步驟如下:
第四步:基于UIViewController創(chuàng)建一個VideoViewController,這個類用來播放視頻

第五步:在VideosViewController(視頻列表頁)里創(chuàng)建UIPageViewController,實(shí)現(xiàn)上下滑動,而且是無限滑動。跟第三步類似,但是參數(shù)設(shè)置不一樣。代碼如下:

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor.blue
        
        // 當(dāng)前類的功能描述
        let desLabel : UILabel = UILabel(frame: self.view.bounds)
        desLabel.numberOfLines = 0
        desLabel.textColor = UIColor.white
        desLabel.textAlignment = NSTextAlignment.center
        desLabel.text = "抖音視頻列表頁\n往右滑到搜索頁,往左滑到用戶詳情頁\n上下滑切換視頻"
        self.view.addSubview(desLabel)
        
        // 創(chuàng)建 UIPageViewController
        let pageVC : UIPageViewController = UIPageViewController.init(transitionStyle: UIPageViewController.TransitionStyle.scroll, navigationOrientation: UIPageViewController.NavigationOrientation.vertical, options: nil)
        let videoVC : VideoViewController = VideoViewController()
        videoVC.videoIndex = self.currentIndex
        pageVC.setViewControllers([videoVC], direction: UIPageViewController.NavigationDirection.forward, animated: false, completion: nil)
        pageVC.dataSource = self
        pageVC.delegate = self
        
        // 將pageVC添加到當(dāng)前VC
        self.addChild(pageVC)
        self.view.addSubview(pageVC.view)
        pageVC.view.frame = self.view.bounds
        pageVC.didMove(toParent: self)
    }
    
    // MARK: - UIPageViewControllerDataSource
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        if self.currentIndex == 1 {
            return nil;
        }
        
        let videoVC : VideoViewController = VideoViewController()
        videoVC.videoIndex = self.currentIndex - 1
        videoVC.view.backgroundColor = UIColor.white
        return videoVC;
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        let videoVC : VideoViewController = VideoViewController()
        videoVC.videoIndex = self.currentIndex + 1
        videoVC.view.backgroundColor = UIColor.white
        return videoVC;
    }

    // MARK: - UIPageViewControllerDelegate
    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        // 簡單的動畫效果
        let videoController : VideoViewController = pageViewController.viewControllers?[0] as! VideoViewController
        self.currentIndex = videoController.videoIndex!
        UIView.animate(withDuration: 2.0) {
            videoController.view.backgroundColor = UIColor.clear
        }
    }

效果圖如下:

仿抖音效果

此工程我已上傳到Github,你的star是我的動力。

最后打個廣告,個人第三方庫:
UDUserDefaultsModel:NSUserDefaults的改進(jìn)方案
YIIFMDB:直接操作Model進(jìn)行增刪改查,數(shù)學(xué)運(yùn)算等,且sql語句易于管理

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在夜晚,你并不看我, 我感到燥熱又冰冷, 像一顆被暴曬的石頭, 丟棄在空空的戈壁—— 這是春天里一個寂寞的夜晚, ...
    三水林楓閱讀 683評論 14 32
  • 一年一度全國矚目的高考終于結(jié)束了。 回顧來路,感恩有你,點(diǎn)滴溫暖。 在高考期間,為了助力莘莘學(xué)子們勝利參加高考,獨(dú)...
    1d0ff747134a閱讀 424評論 4 8
  • 姓名:彭克軍 20180627【日精進(jìn)打卡第天】237天 【知~學(xué)習(xí)】 《六項(xiàng)精進(jìn)》共290遍 《大學(xué)》共295遍...
    彭克軍閱讀 140評論 0 0

友情鏈接更多精彩內(nèi)容