无标题文章

import UIKitclass ViewController: UIViewController {    //屏幕的宽    let kscreenwidth = UIScreen.main.bounds.size.width    //屏幕的高    let kscreenheight = UIScreen.main.bounds.size.height    override func viewDidLoad() {        super.viewDidLoad()      //UIScrollView:滚动视图,是所有滚动视图的基类,只要一个视图能够滚动,要么是UIScrollView,要是UIScrollView的子类,UIScrollView有两个很重要子类:UITableView,UICollectionView      //什么时候才需要滚动:当我们的内容大于可视区域时,为了看到更多内容,才需要滚动去查看        //创建UIScrollView        let scrollview = UIScrollView(frame: CGRect(x: 20, y: 20, width: kscreenwidth - 40, height: kscreenheight - 40))        scrollview.backgroundColor = #colorLiteral(red: 0.9098039269, green: 0.4784313738, blue: 0.6431372762, alpha: 1)        //设置scrollview的内容区域大小        scrollview.contentSize = CGSize(width: 3*kscreenwidth, height: 2*kscreenheight)                        let imageview = UIImageView(frame: CGRect(x: 0, y: 0, width: 3*kscreenwidth, height: 2*kscreenheight))        imageview.image = #imageLiteral(resourceName: "image.jpg")        //设置scrollview的偏移量        //scrollview.contentOffset = CGPoint(x: kscreenwidth, y: 0)        scrollview.addSubview(imageview)        //设置滚动条样式        scrollview.indicatorStyle = .white        //设置是否显示滚动条        scrollview.showsVerticalScrollIndicator = false        //水平滚动条        scrollview.showsHorizontalScrollIndicator = false        //方向锁,滚动的时候只能朝一个方向滚动        scrollview.isDirectionalLockEnabled = true        //设置是否有弹簧效果        //scrollview.bounces = false        //设置是否总是有水平方向有弹簧效果        scrollview.alwaysBounceHorizontal = true        //设置是否总是有垂直方向有弹簧效果        scrollview.alwaysBounceVertical = true        //设置是否支持整页滚动        scrollview.isPagingEnabled = true        //设置scrollview是否支持滚动        //scrollview.isScrollEnabled = false        //设置scrollView是否支持回到顶部        scrollview.scrollsToTop = true        //scrollview的代理属性        scrollview.delegate = self        //设置scrollView的最大最小缩放比例        scrollview.minimumZoomScale = 1.0        self.view.addSubview(scrollview)    }            }    //在延展中管理UIScrollViewDelegate的协议方法    extension ViewController:UIScrollViewDelegate{      //1当scrollView滚动的时候这个方法会持续触发        func scrollViewDidScroll(_ scrollView: UIScrollView) {// any offset changes        print("滚动着,滚动着")            print(scrollView.contentOffset)        }        //2缩放的过程中持续触发        func scrollViewDidZoom(_ scrollView: UIScrollView){ // any zoom scale changes        // called on start of dragging (may require some time and or distance to move)            print("缩放着")            print(scrollView.zoomScale)                                }        //3开始拖拽的时候触发        func scrollViewWillBeginDragging(_ scrollView: UIScrollView)                {// called on finger up if the user dragged. velocity is in points/millisecond. targetContentOffset may be changed to adjust where the scroll view comes to rest                                  print("开始拖拽了")                                }        //4将要结束拖拽的时候触发        func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer)

{// called on finger up if the user dragged. decelerate is true if it will continue moving afterwards

print("要结束拖拽了")

}

//5已经结束拖拽的时候触发

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool){

print("结束拖拽")

}

//6将要开始减速的时候触发

func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView){ // called on finger up as we are moving

print("将要开始减速")

}

//7减速完成,速度为0,这个方法很重要,往往都是在这个方法中获取scrollView偏量

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {// called when scroll view grinds to a halt

print("减速完成")

}

//8给scrollView设置一个结束动画的时候触发,不制定动画就不会触发

func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {// called when setContentOffset/scrollRectVisible:animated: finishes. not called if not animating

}

//9返回scrollView上缩放视图

func viewForZooming(in scrollView: UIScrollView) -> UIView? {// return a view that will be scaled. if delegate returns nil, nothing happens

return scrollView.viewWithTag(200)

}

//10将要开始缩放的时候触发

func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {// called before the scroll view begins zooming its content

print("将要开始缩放的时候触发")

}

//11结束缩放的时候触发

func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {// scale between minimum and maximum. called after any 'bounce' animations

print("结束缩放的时候触发")

}

//12设置点就状态栏是否能回到顶部

func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {// return a yes if you want to scroll to the top. if not defined, assumes YES

return true

}

//13scrollView回到顶部的触发的方法

func scrollViewDidScrollToTop(_ scrollView: UIScrollView){ // called when scrolling animation finished. may be called immediately if already at top

print("scrollView已经回到顶部")

}

}

// override func didReceiveMemoryWarning() {

// super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

// }

//}

你可能感兴趣的:(无标题文章)