Swift - UIScrollView

创建UISecrollView

import UIKit

class ViewController: UIViewController,UIScrollViewDelegate{
    override func viewDidLoad() {
        super.viewDidLoad()
        //创建UIScrollView滚动视图,并设置其显示大小,位置为全屏
        let scrollView = UIScrollView()
        //设置delegate为自己
        scrollView.delegate = self
        //设置UIScrollView显示内容大小
        scrollView.frame = self.view.bounds
        let imageView = UIImageView(image: UIImage(named: "4"))
        //设置UIScrollView的内容大小
        scrollView.contentSize = imageView.bounds.size
        //将图片添加到UIScrollView中
        scrollView.addSubview(imageView)
        //设置UIScrollView的缩放大小(默认缩放小大为1.0)
        scrollView.minimumZoomScale = 0.1
        scrollView.maximumZoomScale = 4
        // 缩放回弹(默认为true,在超出缩放范围会有一个回弹效果)
        scrollView.bouncesZoom = false
        // 设置是否翻页
        scrollView.isPagingEnabled = false
        // 显⽰示⽔水平滚动条,显⽰示垂直滚动条(默认为true)
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        // 滚动条样式
        scrollView.indicatorStyle = UIScrollView.IndicatorStyle.white
        // 设置回弹效果(默认为true)
        scrollView.bounces = false
        // 点击设备状态栏会自动滚动到顶部
        scrollView.scrollsToTop = true
        
        
        self.view.addSubview(scrollView)
    }

    //scrollView滚动时调用,只要offset的值发生变化就调用
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //打印视图偏移量
        print("X:\(scrollView.contentOffset.x) Y:\(scrollView.contentOffset.y)")
    }
    //返回将要缩放的UIView对象。
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        for subview:AnyObject in scrollView.subviews{
            if subview.isKind(of: UIImageView.self){
                return subview as? UIView
            }
        }
        return nil
    }

    //当将要开始缩放时,执行该方法。一次有效缩放就只执行一次
    func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
    }
    //当scrollView缩放时,调用该方法。在缩放过程中,会多次调用
    func scrollViewDidZoom(_ scrollView: UIScrollView) {
      
    }
    //当缩放结束后,并且缩放大小回到minimumZoomScale与maximumZoomScale之间后(我们也许会超出缩放范围),调用该方法
    func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
    
    }
    //指示当用户点击状态栏后,滚动视图是否能够滚动到顶部
    func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {
        return true
    }
    //当滚动视图滚动到最顶端后,执行该方法
    func scrollViewDidScrollToTop(_ scrollView: UIScrollView) {
       
    }
    //当开始滚动视图时,执行该方法。一次有效滑动只执行一次。
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        
    }
    //当开始滚动视图时,执行该方法。一次有效滑动只执行一次。
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
      
    }
    //滑动减速时调用该方法
    func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
        
    }
    //滚动视图减速完成,滚动将停止时,调用该方法。一次有效滑动只执行一次
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
       
    }
    //当滚动视图动画完成后,调用该方法,如果没有动画,那么该方法将不被调用
    func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {

    }
    //滑动scrollView,并且手指离开时执行。一次有效滑动只执行一次(当pagingEnabled属性为true时,不调用该方法)
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer) {
       
    }

}

实现页面滚动切换

ViewController:

import UIKit

class ViewController: UIViewController,UIScrollViewDelegate{
    override func viewDidLoad() {
        super.viewDidLoad()
        let scrollView = UIScrollView()
        scrollView.frame = self.view.bounds
        print(scrollView.bounds.width)
        scrollView.contentSize = CGSize(width: self.view.bounds.width * 2, height: scrollView.bounds.height)
        scrollView.isPagingEnabled = true
        //添加子页面
        let firstView = FirstViewController()
        //设置图片播放的页数
        firstView.view.frame = CGRect(x:0, y: 0, width: scrollView.bounds.width, height: scrollView.bounds.height)
        scrollView.addSubview(firstView.view)
        let secondView = SecondViewController()
        //设置图片播放的页数
        secondView.view.frame = CGRect(x: self.view.bounds.width, y: 0, width: scrollView.bounds.width, height: scrollView.bounds.height)
        scrollView.addSubview(secondView.view)
        self.view.addSubview(scrollView)
        }
    }

创建两个swift文件,命名为
FirstViewControll,SeconfViewController:

import UIKit
class FirstViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.red
    }
}

两个文件只有背景颜色设置不一样

你可能感兴趣的:(Swift - UIScrollView)