UIScrollView的详细使用
创建实例对象并初始化
let scrollView = UIScrollVeiw(frame: self.view.frame)
let subViewOne = UIView(frame: self.view.frame)
let subViewTwo = UIView(frame: CGReact(x: self.view.frame.size.width, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
scrollView.addSubView(subViewOne)
scrollView.addSubView(subViewTwo)
scrollView.contentSize = CGSize(width: self.view.frame.size.width * 2, height: self.view.frame.size.height)
scrollView.alwaysBounceVertical = true
scrollView.alwaysBounceHorizontal = true
scrollView.bouns = true
scrollView.showsVerticalScrollIndicator = true
scrollView.showsHorizontalScrollIndicator = false
scrollView.isPagingEnabled = true
常见的代理方法
1 滑动过程中 scrollViewDidScroll(scrollView: UIScrollView)
2 开始拖动 scrollViewWillBeginDragging(scrollView: UIScrollView)
3 开始拖动(以某种速率和偏移量) scrollViewDidEndDecelerating(scrollView: UIScrollView)
4 停止拖动 scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool)
5 开始滑动 scrollViewWillBeginDecelerating(scrollView: UIScrollView)
代码如下:
//
// ViewController.swift
// ScrollView
//
// Created by swift on 2019/12/31.
// Copyright © 2019 kydqitb. All rights reserved.
//
importUIKit
class ViewController: UIViewController, UIScrollViewDelegate {
letview1=UIView()
letview2=UIView()
letview3=UIView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
addTitleLabel()
setViews()
addScrollView()
}
func addTitleLabel() {
lettitle =UILabel(frame:CGRect(x:0, y:40, width:self.view.frame.width, height:40))
title.textAlignment = .center
title.text = "UIScrollView基础使用"
self.view.addSubview(title)
}
func addScrollView() {
letscrollView =UIScrollView()
view.addSubview(scrollView)
addAutoLayout(obj: scrollView)
scrollView.delegate=self;
//设置scrollView的背景颜色
scrollView.backgroundColor=UIColor.red
scrollView.contentSize=CGSize(width:view.frame.width, height:500*4)// ContentSize属性,滑动内容宽度和高度
//修改滑动光标的颜色
/**
default // 默认灰色广标
black // 黑色光标
white // 白色光标
*/
scrollView.indicatorStyle = UIScrollView.IndicatorStyle.black
//true滑动到边缘时光标具有反弹效果(光标变短)
scrollView.bounces=true
//分页效果 每次移动一个格
scrollView.contentOffset=CGPoint(x:50, y:500)
// 显⽰示⽔水平滚动条(默认为true)
scrollView.showsHorizontalScrollIndicator = true
//显⽰示垂直滚动条(默认为true)
scrollView.showsVerticalScrollIndicator = true
//设置UIScrollView的缩放大小(默认缩放小大为1.0)
scrollView.minimumZoomScale=0.2
scrollView.maximumZoomScale=2
// 缩放回弹(默认为true,在超出缩放范围会有一个回弹效果)
scrollView.bouncesZoom=true
// 点击设备状态栏会自动滚动到顶部
scrollView.scrollsToTop=true
//把view添加到scrollView上
scrollView.addSubview(view1)
scrollView.addSubview(view2)
scrollView.addSubview(view3)
}
func setViews() {
view1.backgroundColor = UIColor.gray
view1.frame=CGRect(x:0, y:0, width:view.frame.width, height:500)//此约束相对于scrollView.contentSize大小进行约束
view2.backgroundColor = UIColor.green
view2.frame=CGRect(x:0, y:500, width:view.frame.width, height:500)
view3.backgroundColor = UIColor.blue
view3.frame=CGRect(x:0, y:1000, width:view.frame.width, height:500)
}
//MARK: - scrollView代理方法
//scrollView滚动时调用,只要offset的值发生变化就调用
funcscrollViewDidScroll(_scrollView:UIScrollView) {
//打印视图偏移量
print("X:\(scrollView.contentOffset.x)")
print("Y:\(scrollView.contentOffset.y)")
// print("Y:\()")
}
//返回将要缩放的UIView对象
funcviewForZooming(in scrollView:UIScrollView) ->UIView? {
print("viewForZooming")
//遍历scrollView的子view,找到自己的view
forsubview:AnyObjectinscrollView.subviews{
ifsubview.isKind(of:UIView.self){
returnsubviewas?UIView
}
}
returnnil
}
//当将要开始缩放时,执行该方法。一次有效缩放就只执行一次
funcscrollViewWillBeginZooming(_scrollView:UIScrollView, with view:UIView?) {
print("scrollViewWillBeginZooming")
}
//当scrollView缩放时,调用该方法。在缩放过程中,会多次调用
funcscrollViewDidZoom(_scrollView:UIScrollView) {
print("scrollViewDidZoom")
}
//当缩放结束后,并且缩放大小回到minimumZoomScale与maximumZoomScale之间后(我们也许会超出缩放范围),调用该方法
funcscrollViewDidEndZooming(_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")
}
//当开始滚动视图时,执行该方法。一次有效滑动只执行一次
funcscrollViewDidEndDragging(_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时,不调用该方法)
funcscrollViewWillEndDragging(_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)
let objBottom:NSLayoutConstraint = NSLayoutConstraint(item: obj, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.view, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1.0, constant: -100)
obj.superview!.addConstraint(objBottom)
// //设置label控件的高度为200pt
// let objHeight:NSLayoutConstraint = NSLayoutConstraint(item: obj, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute:NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1.0, constant: 400)
// obj.superview!.addConstraint(objHeight)
}
}
网友评论