Swift UIScrollView基础写法

// ViewController.swift
// ScrollView

// Created byABC on 2021/5/21.
//
import UIKit

class ViewController: UIViewController, UIScrollViewDelegate {

    let view1 = UIView()
    let view2 = UIView()
    let view3 = UIView()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        addTitleLabel()
        
        setViews()
        
        addScrollView()
    }

    func addTitleLabel() {
        let title = UILabel(frame: CGRect(x: 0, y: 40, width: self.view.frame.width, height: 40))
        title.textAlignment = .center
        title.text = "启动页"
        self.view.addSubview(title)
    }
    
    func addScrollView() {
        let scrollView = UIScrollView()
        view.addSubview(scrollView)
        //addAutoLayout(obj: scrollView)
        
        scrollView.delegate = self;
        //设置scrollView的背景颜色
        scrollView.backgroundColor = UIColor.white
        scrollView.frame = CGRect(x: 0, y: 40, width: self.view.frame.width, height: self.view.frame.height)
    
      //  scrollView.contentSize = CGSize(width: view.frame.width, height:self.view.frame.height) // ContentSize属性,滑动内容宽度和高度
        
        //修改滑动光标的颜色
        /**
          default // 默认灰色广标
          black // 黑色光标
          white // 白色光标
         */
        scrollView.indicatorStyle = UIScrollView.IndicatorStyle.black
        
        //true滑动到边缘时光标具有反弹效果(光标变短)
        scrollView.bounces = false
        
        //分页效果 每次移动一个格
//        scrollView.isPagingEnabled = true

   //     scrollView.contentOffset = CGPoint(x: 50, y: 500)
        
        // 显⽰示⽔水平滚动条(默认为true)
        scrollView.showsHorizontalScrollIndicator = false
        //显⽰示垂直滚动条(默认为true)
        scrollView.showsVerticalScrollIndicator = false
        
        //设置UIScrollView的缩放大小(默认缩放小大为1.0)
       // scrollView.minimumZoomScale = 0.2
        //scrollView.maximumZoomScale = 2
        // 缩放回弹(默认为true,在超出缩放范围会有一个回弹效果)
        scrollView.bouncesZoom = false
        
        // 点击设备状态栏会自动滚动到顶部
        scrollView.scrollsToTop = false
        //把view添加到scrollView上
        scrollView.addSubview(view1)
       
    }
    
    func setViews() {
        view1.backgroundColor = UIColor.yellow
        view1.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height) //此约束相对于scrollView.contentSize大小进行约束
        
    }
    
    
    //MARK: - scrollView代理方法
    //scrollView滚动时调用,只要offset的值发生变化就调用
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //打印视图偏移量
        print("X:\(scrollView.contentOffset.x)")
        print("Y:\(scrollView.contentOffset.y)")
    }
    //返回将要缩放的UIView对象
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        print("viewForZooming")
        //遍历scrollView的子view,找到自己的view
        for subview:AnyObject in scrollView.subviews{
            if subview.isKind(of: UIView.self){
                return subview as? UIView
            }
        }
        return nil
    }
    
    //当将要开始缩放时,执行该方法。一次有效缩放就只执行一次
    func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
        print("scrollViewWillBeginZooming")
    }
    
    //当scrollView缩放时,调用该方法。在缩放过程中,会多次调用
    func scrollViewDidZoom(_ scrollView: UIScrollView) {
        print("scrollViewDidZoom")
    }
    //当缩放结束后,并且缩放大小回到minimumZoomScale与maximumZoomScale之间后(我们也许会超出缩放范围),调用该方法
    func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
        print("scrollViewDidZoom scale: %f", scale)
    }
    //指示当用户点击状态栏后,滚动视图是否能够滚动到顶部
    func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {
        print("scrollViewShouldScrollToTop")
        //返回值:true可以返回到顶端, false不能返回到顶端
        return true
    }
    //当滚动视图滚动到最顶端后,执行该方法
    func scrollViewDidScrollToTop(_ scrollView: UIScrollView) {
        print("scrollViewDidScrollToTop")
    }
    
    //当开始滚动视图时,执行该方法。一次有效滑动只执行一次
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        print("scrollViewWillBeginDragging")
    }
    //当开始滚动视图时,执行该方法。一次有效滑动只执行一次
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        print("scrollViewDidEndDragging")
    }
    //滑动减速时调用该方法
    func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
        print("scrollViewWillBeginDecelerating")
    }
    //滚动视图减速完成,滚动将停止时,调用该方法。一次有效滑动只执行一次
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        print("scrollViewDidEndDecelerating")
    }
    //当滚动视图动画完成后,调用该方法,如果没有动画,那么该方法将不被调用
    func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
        print("scrollViewDidEndScrollingAnimation")
    }
    //滑动scrollView,并且手指离开时执行。一次有效滑动只执行一次(当pagingEnabled属性为true时,不调用该方法)
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer) {
        print("scrollViewWillEndDragging")
    }
    
    
    
    
    
    //AutoLayout 自动布局
    func addAutoLayout(obj: UIScrollView) {
    //除了AutoLayout,AutoresizingMask也是一种布局方式。当我们用AutoLayout代码添加视图时默认情况下,translatesAutoresizingMaskIntoConstraints = true , 此时视图的AutoresizingMask会被转换成对应效果的约束。这样很可能就会和我们手动添加的其它约束有冲突。此属性设置成false时,AutoresizingMask就不会变成约束。也就是说当前视图的 AutoresizingMask失效了。 避免布局冲突,把此设置为false
        obj.translatesAutoresizingMaskIntoConstraints = false
        
        //        自动布局构造函数
        //        NSLayoutConstraint(item: 视图,
        //                           attribute: 约束属性,
        //                           relatedBy: 约束关系,
        //                           toItem: 参照视图,
        //                           attribute: 参照属性,
        //                           multiplier: 乘积,
        //                           constant: 约束数值)
        //设置label控件的左边到self.view的左边距离为50pt
        let objLeft:NSLayoutConstraint = NSLayoutConstraint(item: obj, attribute: NSLayoutConstraint.Attribute.left, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.view, attribute: NSLayoutConstraint.Attribute.left, multiplier: 1.0, constant: 50)
        obj.superview!.addConstraint(objLeft)
        
        //设置label控件的顶部到self.view的顶部距离为100pt
        let objTop:NSLayoutConstraint = NSLayoutConstraint(item: obj, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.view, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1.0, constant: 100)
        obj.superview!.addConstraint(objTop)
        
        //设置label控件的右边到self.view的右边距离为50pt
        let objRight:NSLayoutConstraint = NSLayoutConstraint(item: obj, attribute: NSLayoutConstraint.Attribute.right, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.view, attribute: NSLayoutConstraint.Attribute.right, multiplier: 1.0, constant: -50)
        obj.superview!.addConstraint(objRight)
        
        //设置label控件的高度为500pt
        let objHeight:NSLayoutConstraint = NSLayoutConstraint(item: obj, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute:NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1.0, constant: 500)
        obj.superview!.addConstraint(objHeight)
    }


}

你可能感兴趣的:(Swift UIScrollView基础写法)