IOS引导页面测试

2016-07-23  本文已影响31人  小张呵呵

转载:http://www.jianshu.com/p/024dd2d6e6e6#




import UIKit

class ViewController: UIViewController {
    //storyboard里面的开始按钮
    @IBOutlet weak var startButton: UIButton!
    //storyboard里面的
    @IBOutlet weak var pageCon: UIPageControl!
    // 新建UIScrollView
    private var scrollView: UIScrollView!
    // UIScrollView的滚动数量
    private let numOfPages = 3
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        //获取vc大小
        let frame = self.view.bounds
        //大小
        scrollView = UIScrollView(frame: frame)
        
        scrollView.pagingEnabled = true
        
        scrollView.showsHorizontalScrollIndicator = false
        
        scrollView.showsVerticalScrollIndicator = false
        
        scrollView.scrollsToTop = false
        //滑动到首尾的弹性效果
        scrollView.bounces = false
        
        scrollView.contentOffset = CGPointZero
        // 将 scrollView 的 contentSize 设为屏幕宽度的3倍(根据实际情况改变)
        scrollView.contentSize = CGSize(width: frame.size.width * CGFloat(numOfPages), height: frame.size.height)
        //滑动的代理
        scrollView.delegate = self
        //设置导航的页面
        for index  in 0..<numOfPages {
            let imageView = UIImageView(image: UIImage(named: "\(index + 1)"))
            imageView.frame = CGRect(x: frame.size.width * CGFloat(index), y: 0, width: frame.size.width, height: frame.size.height)
            scrollView.addSubview(imageView)
        }
        //添加到vc
        self.view.insertSubview(scrollView, atIndex: 0)
        // 给开始按钮设置圆角
        startButton.layer.cornerRadius = 11.0
        // 隐藏开始按钮
        startButton.alpha = 0.0
    }
    
    // 隐藏状态栏
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
}

// MARK: - UIScrollViewDelegate
extension ViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(scrollView: UIScrollView) {
        //滑动的当前页面
        let offset = scrollView.contentOffset
        // 随着滑动改变pageControl的状态
        pageCon.currentPage = Int(offset.x / view.bounds.width)
        
        // 因为currentPage是从0开始,所以numOfPages减1
        if pageCon.currentPage == numOfPages - 1 {
            UIView.animateWithDuration(0.5) {
                self.startButton.alpha = 1.0
            }
        } else {
            UIView.animateWithDuration(0.2) {
                self.startButton.alpha = 0.0
            }
        }
    }
}

上一篇下一篇

猜你喜欢

热点阅读