美文网首页iOS 引导页iOS开发精进iOS开发
iOS开发 启动引导页 SwiftIntroView

iOS开发 启动引导页 SwiftIntroView

作者: 小黑Swift | 来源:发表于2016-03-23 17:55 被阅读604次
Simulator Screen Shot 2016年3月23日 下午5.39.15.png 搬砖累,心更累

可根据需要自己增加控件

使用:注意逻辑关系,有些视图存在依赖关系

SwiftIntroView.swift
import UIKit

// 代理
protocol SwiftIntroViewDelegate:AnyObject {
    func doneButtonClick()
}

class SwiftIntroView: UIView, UIScrollViewDelegate {
    
    var scrollView: UIScrollView!
    var pageControl: UIPageControl!
    var doneButton: UIButton!
    
    var view1: UIImageView!
    var view2: UIImageView!
    var view3: UIImageView!
    var view4: UIImageView!
    
    var delegate: SwiftIntroViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //注意逻辑关系,有些视图依赖前一个视图而存在
        self.initScrollView()
        self.initPageViews()
        self.initPageControl()
        self.initDoneButton()
    }
    
    //初始化 scrollView
    func initScrollView() {
        scrollView = UIScrollView(frame: self.frame)
        scrollView.delegate = self
        scrollView.pagingEnabled = true
        scrollView.contentSize = CGSize(width: self.frame.size.width * 4, height: scrollView.frame.size.height)
        scrollView.contentOffset = CGPoint(x: 0, y: 0)
        self.addSubview(scrollView)
    }
    
    //初始化页面视图 - 可根据需要自己增加控件
    func initPageViews() {
        let originW = self.frame.size.width

        view1 = UIImageView(frame: self.frame)
        view1.frame.origin.x = originW * 0
        
        view2 = UIImageView(frame: self.frame)
        view2.frame.origin.x = originW * 1
        
        view3 = UIImageView(frame: self.frame)
        view3.frame.origin.x = originW * 2
        
        view4 = UIImageView(frame: self.frame)
        view4.frame.origin.x = originW * 3
        
        view1.image = UIImage(named: "imageName1")
        view2.image = UIImage(named: "imageName2")
        view3.image = UIImage(named: "imageName3")
        view4.image = UIImage(named: "imageName4")

        self.scrollView.addSubview(view1)
        self.scrollView.addSubview(view2)
        self.scrollView.addSubview(view3)
        self.scrollView.addSubview(view4)
    }
    
    //初始化 pageControl
    func initPageControl() {
        pageControl = UIPageControl(frame: CGRect(x: 0, y: self.frame.size.height - 80, width: self.frame.size.width, height: 10))
        pageControl.currentPageIndicatorTintColor = UIColor(red: 33/255, green: 150/255, blue: 243/255, alpha: 0.8)
        pageControl.numberOfPages = 4
        self.addSubview(pageControl)
    }
    
    //初始化 DoneButton
    func initDoneButton() {
        doneButton = UIButton(frame: CGRect(x: 0, y: self.frame.size.height - 60, width: self.frame.size.width, height: 60))
        doneButton.setTitle("跳过", forState: .Normal)
        doneButton.titleLabel?.font = UIFont.systemFontOfSize(18)
        doneButton.backgroundColor = UIColor(red: 33/255, green: 150/255, blue: 243/255, alpha: 0.5)
        //增加点击事件并交给代理去完成
        doneButton.addTarget(self.delegate, action: "doneButtonClick", forControlEvents: .TouchUpInside)
        self.addSubview(doneButton)
    }
    
    //实现 UIScrollViewDelegate 方法
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let pageWidth = self.frame.width
        let pageFraction = self.scrollView.contentOffset.x / pageWidth
        self.pageControl.currentPage = Int(roundf(Float(pageFraction)))
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

ViewController.swift
import UIKit

class ViewController: UIViewController, SwiftIntroViewDelegate {
    
    var introView: SwiftIntroView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        introView = SwiftIntroView(frame: self.view.frame)
        introView.delegate = self
        introView.backgroundColor = UIColor(red: 33/255, green: 150/255, blue: 243/255, alpha: 1)
        self.view.addSubview(introView)
        
    }
    
    // SwiftIntroViewDelegate 方法
    func doneButtonClick() {
        
        UIView.animateWithDuration(1, delay: 0, options: .CurveEaseInOut, animations: { () -> Void in
                self.introView.alpha = 0
            }) { (finished) -> Void in
                self.introView.removeFromSuperview()
                self.createdMainView()
        }   
    }
    
    func createdMainView () {
        let dragView = UIImageView(frame: self.view.frame)
        dragView.image = UIImage(named: "imageName")
        dragView.alpha = 0
        view.addSubview(dragView)
        UIView.animateWithDuration(2, delay: 0, options: .CurveEaseInOut, animations: { () -> Void in
            dragView.alpha = 1
            }) { (finished) -> Void in
                
        }
    }
}

判断是否显示引导视图

如果想加入只在用户第一次启动时候显示,可更改上面 viewDidLoad 里面的代码为:

        let userDefaults = NSUserDefaults.standardUserDefaults()
        if userDefaults.valueForKey("showIntroView") == nil {
           userDefaults.setValue("showView", forKey: "showIntroView")
            
            introView = SwiftIntroView(frame: self.view.frame)
            introView.delegate = self
            introView.backgroundColor = UIColor(red: 33/255, green: 150/255, blue: 243/255, alpha: 1)
            self.view.addSubview(introView)
        } else {
            createdDragView()
        }

相关文章

网友评论

    本文标题:iOS开发 启动引导页 SwiftIntroView

    本文链接:https://www.haomeiwen.com/subject/rmallttx.html