swift中UIScrollView的使用

2016-12-21  本文已影响0人  不安分心

UIScrollView的使用

import UIKit

class ViewController: UIViewController {

    var pageControl: UIPageControl?
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.setupScrollView()
    }
    func setupScrollView() {
    
        let scrollView = UIScrollView(frame: CGRect(x: 0, y: 100, width: UIScreen.main.bounds.width, height: 300))
        scrollView.backgroundColor = UIColor.red
        // 设置滚动条
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        // 设置分页滚动
        scrollView.isPagingEnabled = true
        // 设置是否可以拉出空白区域
        scrollView.bounces = false
        // 垂直
        scrollView.alwaysBounceVertical = false
        // 水平
        scrollView.alwaysBounceHorizontal = false
        // 是否滚动
        scrollView.isScrollEnabled = true
        // 在scrollView的内容周围添加一个附件区域
//        scrollView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10)
        // 调整指示器(滚动条)的位置
//        scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(30, 30, 30, 30)
        // 滚动条的样式
        scrollView.indicatorStyle = .black
        // 最小的缩放倍数,默认值为1.0
        scrollView.minimumZoomScale = 0.2
        // 最大的缩放倍数,默认值为1.0
        scrollView.maximumZoomScale = 100
        
        // 添加图片
        let imagesArray = ["0.jpg", "1.jpg", "2.pg"]
        for i in 0..<imagesArray.count {
            // 创建imageview
            let imageView = UIImageView(frame: CGRect(x: CGFloat(i)*scrollView.frame.width, y: 0, width: scrollView.frame.width, height: scrollView.frame.height))
            let imageName = imagesArray[i]
            imageView.image = UIImage(named: imageName)
            imageView.isUserInteractionEnabled = true
            scrollView.addSubview(imageView)
        }
        // 设置内容区域
        scrollView.contentSize = CGSize(width: scrollView.frame.width*CGFloat(imagesArray.count), height: scrollView.frame.height)
        scrollView.delegate = self
        scrollView.isDirectionalLockEnabled = false
        // 设置偏移量
//        scrollView.setContentOffset(CGPoint(x: 100, y: 10), animated: true)
        // 创建UIPageControl
        let pageControl = UIPageControl(frame: CGRect(x: 100, y: UIScreen.main.bounds.height-50, width: 200, height: 30))
//        pageControl.backgroundColor = UIColor.red
        // 总的图片页数
        pageControl.numberOfPages = 3
        // 当前页
        pageControl.currentPage = 0
        pageControl.currentPageIndicatorTintColor = UIColor.orange
        // pageControl响应函数
        pageControl.addTarget(self, action: #selector(pageControlClick(_:)), for: .valueChanged)
        self.view.addSubview(pageControl)
        self.pageControl = pageControl
        self.view.addSubview(scrollView)
    }
    func pageControlClick(_ pageControl: UIPageControl) {
    
    }
}

// MARK: UIScrollViewDelegate
extension ViewController: UIScrollViewDelegate {
    
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        print("将要开始拖动")
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        print("已经滑动")
        if scrollView.contentOffset.x < scrollView.frame.width {
            self.pageControl?.currentPage = 0
        }
        else if (scrollView.contentOffset.x < 2*scrollView.frame.width) && (scrollView.contentOffset.x >= scrollView.frame.width) {
            self.pageControl?.currentPage = 1
        }
        else {
            self.pageControl?.currentPage = 2
        }
    }
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        print("停止拖动")
    }
    func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {
        print("将要滚动到顶部")
        return true
    }
    func scrollViewDidScrollToTop(_ scrollView: UIScrollView) {
        print("已经滚动到顶部")
    }
    func scrollViewDidZoom(_ scrollView: UIScrollView) {
        print("正在缩放")
    }
    func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
        print("以及缩放完毕")
    }
}
上一篇下一篇

猜你喜欢

热点阅读