ios 仿keep的登錄注冊頁面實現

GuidePage

本來是做個引導圖呢,受到keepDemo的啟發,發現它們的注冊登錄頁效果感覺挺高大上,也給其他app登錄注冊提供了一種設計思路,即用視屏作頁面背景。

先上效果圖

GuidePage.gif

代碼比較簡單,用到的控件也比較少,話不多少,用的swift,直接上代碼!

code

import UIKit
import AVFoundation
import AVKit

let kWidth = UIScreen.mainScreen().bounds.width
let kHeight = UIScreen.mainScreen().bounds.height
    override func viewDidLoad() {
        super.viewDidLoad()
        let filePath = NSBundle.mainBundle().pathForResource("1", ofType: "mp4")
        let url = NSURL(fileURLWithPath: filePath!)
        moviePlayController = AVPlayerViewController()
        player = AVPlayer(URL: url)
        moviePlayController.player = player
        moviePlayController.view.frame = self.view.bounds
        self.view.addSubview(moviePlayController.view)
        moviePlayController.showsPlaybackControls = false
        player.play()
    
        bgAlphaView.frame = moviePlayController.view.frame
        moviePlayController.view.addSubview(bgAlphaView)
        settingScrollView()
        settingTimer()
        
        //repate play
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.repeatPlay(_:)), name: AVPlayerItemDidPlayToEndTimeNotification, object: nil)
        
    }   

func settingScrollView() -> Void {
        scrollView.bounces = false
        scrollView.pagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.delegate = self
        scrollView.contentSize = CGSizeMake(kWidth * 4, scrollView.bounds.height)
        for index in 0..<4 {
            let label = UILabel()
            label.textAlignment = .Center
            label.frame.size = CGSizeMake(kWidth, 40)
            label.frame.origin = CGPointMake(CGFloat(index) * kWidth, scrollView.bounds.height - 60)
            label.textColor = UIColor.whiteColor()
            label.text = labels[index]
            scrollView.addSubview(label)
        }
        pageControl.currentPage = 0
    }


    func settingTimer(){
        timer = NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: #selector(self.changePage), userInfo: nil, repeats: true)
    }
    
    func changePage() {
       let page = (pageControl.currentPage + 1) % 4
       pageControl.currentPage = page
       scrollView.setContentOffset(CGPointMake(CGFloat(page) * kWidth, 0), animated: true)
    }
    
    func repeatPlay(noti : NSNotification){
        print("播放完成")
        //第幾秒 = value / timescale
        player.seekToTime(CMTime(value: 0, timescale: 1))
        player.play()
        
    }
extension ViewController : UIScrollViewDelegate{
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / kWidth)
        pageControl.currentPage = page
    }
    
    func scrollViewWillBeginDragging(scrollView: UIScrollView) {
        guard  let _ = timer else {
            return
        }
        timer.invalidate()
        timer = nil
    }
    
    func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        settingTimer()
    }
    
}

總結

代碼比較簡單,大家應該都能看的懂,由于MPMovieplayerController已經在ios9.0中被廢棄了,用來替代的是AVPlayerViewcontroller,至于循環播放,MPMovieplayerController可以setRepeatMode為MPMovieRepeatModeOne,而AVPlayerViewcontroller沒有找到這個屬性,所以只能通過監聽通知來實現了。
對AVkit框架有興趣的同學可以查查apple的api,其實功能很強大!
github源碼地址:[請點這里][id]
[id]:https://github.com/zhuchen1990/testGuidePage

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容