swift中UIScrollView的使用

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..= 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("以及缩放完毕")
    }
}

你可能感兴趣的:(swift中UIScrollView的使用)