UIScrollView

用Swift写的UIScrollView文本

UIScrollView

  • 创建 UIScrollView

    1
    let scrollView = UIScrollView(frame: self.view.bounds)
  • 设置 UIScrollView 的大小

    1
    scrollView.contentSize = CGSize(width: self.view.frame.width * 4, height: self.view.frame.height)
  • 设置 UIScrollView 的偏移位置

    1
    scrollView.contentOffset = CGPoint(x: self.view.frame.width, y: 0)
  • 设置是否反弹

    1
    scrollView.bounces = true
  • 设置是否分页

    1
    scrollView.isPagingEnabled = true
  • 设置是否滚动

    1
    scrollView.isScrollEnabled = true
  • 设置 indicator( 滚动条的颜色 ) 风格

    1
    scrollView.indicatorStyle = UIScrollViewIndicatorStyle.white
  • 设置 内容的边缘和 Indicators 边缘

    1
    scrollView.contentInset = UIEdgeInsetsMake(0, 50, 0, 0)
  • 手势放大缩小

    • .minimumZoomScale = 0.1 最小比例
    • .maximumZoomScale = 3 最大比例
    1
    2
    3
    scrollView.minimumZoomScale = 0.1
    scrollView.maximumZoomScale = 3
  • 设置代理

    1
    scrollView.delegate = self
  • 添加到父视图

    1
    self.view.addSubview(scrollView)

代理方法

  • 返回一个放大或者缩小的视图

    1
    2
    3
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
    }
  • 开始放大或者缩小

    1
    2
    3
    func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
    }
  • 缩放结束时

    1
    2
    3
    func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
    }
  • 视图已经放大或缩小

    1
    2
    3
    func scrollViewDidZoom(_ scrollView: UIScrollView) {
    }
  • 滑动到顶部时调用该方法

    1
    2
    3
    4
    5
    func scrollViewDidScrollToTop(_ scrollView: UIScrollView) {
    print("顶部")
    }
  • 正在滑动

    1
    2
    3
    4
    5
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
    print("正在滑动...")
    }
  • 开始拖动

    1
    2
    3
    4
    5
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    print("开始拖动")
    }
  • 结束拖动

    1
    2
    3
    4
    5
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    print("结束拖动")
    }
  • 开始减速

    1
    2
    3
    4
    5
    func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
    print("开始减速...")
    }
  • 减速停止

    1
    2
    3
    4
    5
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    print("减速停止")
    }
坚持原创技术分享,您的支持将鼓励我继续创作!