6.2、UIScrollView

import UIKit

class ViewController: UIViewController,UIScrollViewDelegate {
let MainWidth = UIScreen.main.bounds.width
var ascrollViews = UIScrollView()
var pageControl:UIPageControl?
override func viewDidLoad() {
super.viewDidLoad()
//基础知识
self.createScrollViews()
}
//基础知识
func createScrollViews() {
let scrollvc = UIScrollView(frame: CGRect(x: 0, y: 64, width: MainWidth, height: 300))
self.view.addSubview(scrollvc)
self.ascrollViews = scrollvc
scrollvc.backgroundColor = UIColor.gray
//显示水平指示条
scrollvc.showsHorizontalScrollIndicator = true
//调整指示器(滚动条)的位置
// scrollvc.scrollIndicatorInsets = UIEdgeInsetsMake(20, 50, 50, 50)
//设置指示器(滚动条)的样式
scrollvc.indicatorStyle = UIScrollViewIndicatorStyle.black
scrollvc.delegate = self
//但是可以设定,内容就算小于它,也能拖:拖着任意方向
// scrollvc.alwaysBounceVertical = true //还有水平的
//显示垂直指示条
scrollvc.showsVerticalScrollIndicator = false
//设置是否可以拉出空白区域、回弹
// scrollvc.bounces = true
//设置分页滚动
scrollvc.isPagingEnabled = true
//默认是false。如果是true并且bounces也是true,即使内容尺寸比scrollView的尺寸小,也能水平推动
scrollvc.alwaysBounceHorizontal = true
//允许滑动视图本身,如果设为false就不能触发拖动代理事件
scrollvc.isScrollEnabled = true
//在scrollView的内容周围添加一个附件的区域
// scrollvc.contentInset = UIEdgeInsetsMake(120, 30, 30, 80)
//设置内容区域大小
scrollvc.contentSize = CGSize(width: MainWidth*3, height: 300)

    let imageView = UIImageView.init(frame: CGRect(x: 0, y: 10, width: MainWidth, height: 290))
    scrollvc.addSubview(imageView)
    imageView.image = #imageLiteral(resourceName: "image12")
    let tap1 = UITapGestureRecognizer(target: self, action: #selector(modelAction))
    imageView.addGestureRecognizer(tap1)
    
    let imageView2 = UIImageView.init(frame: CGRect(x: MainWidth, y: 10, width: MainWidth, height: 290))
    scrollvc.addSubview(imageView2)
    imageView2.image = #imageLiteral(resourceName: "image13")
    imageView2.isUserInteractionEnabled = true
    let tap = UITapGestureRecognizer(target: self, action: #selector(modelAction))
    imageView2.addGestureRecognizer(tap)
    
    let imageView23 = UIImageView.init(frame: CGRect(x: MainWidth*2, y: 10, width: MainWidth, height: 290))
    scrollvc.addSubview(imageView23)
    imageView23.image = #imageLiteral(resourceName: "image15")

    // 缩小的最小比例

// scrollvc.minimumZoomScale = 0.3
// 放大的最大比例
// scrollvc.maximumZoomScale = 5.6

    pageControl = UIPageControl(frame: CGRect(x: 10, y: 334, width: MainWidth-20, height: 30))
    pageControl?.backgroundColor = UIColor.red
    //设置显示的页数
    pageControl?.numberOfPages = 3
    //设置显示的起始页的索引
    pageControl?.currentPage = 0
    //设置单页时隐藏
    pageControl?.hidesForSinglePage = true
    //设置显示颜色
    pageControl?.currentPageIndicatorTintColor = UIColor.green
    //设置页背景指示颜色
    pageControl?.pageIndicatorTintColor = UIColor.lightGray
    //添加事件
    pageControl?.addTarget(self, action: #selector(pageControlChanged), for: .valueChanged)
    self.view.addSubview(pageControl!)
}
// 1、已经开始滚动(不管是拖、拉、放大、缩小等导致)都会执行此函数
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    print("已经开始滚动(不管是拖、拉、放大、缩小等导致)都会执行此函数")
    let offset = scrollView.contentOffset.x / MainWidth
    pageControl?.currentPage = Int(offset)
}
// 2、将要开始拖拽,手指已经放在view上并准备拖动的那一刻
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    print("将要开始拖拽,手指已经放在view上并准备拖动的那一刻")
}
// 3、将要结束拖拽,手指已拖动过view并准备离开手指的那一刻
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    print("将要结束拖拽,手指已拖动过view并准备离开手指的那一刻-----")
}
// 4、已经结束拖拽,手指刚离开view的那一刻
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    print("----------------已经结束拖拽,手指刚离开view的那一刻")
}
func modelAction() {
    let other = OtherViewController()
    self.present(other, animated: true) { 
    }
}
//pageControl的触发事件
func pageControlChanged(pageControl:UIPageControl) {
    let page = pageControl.currentPage
    print("当前显示的是第\(page)页")
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 【为你,千千万万遍】 【课程】 1.晨诵复习之前的,并且让孩子们都写到本子上。 2.用一节课讲完《鲁滨孙漂流记》,...
    实颖实栗阅读 335评论 0 1
  • 问题1.Text对象存储的是引用,如果改变对象值,则一切指向引用的值也跟着改变 可以看到第一次map存储的值是09...
    思索人生_mind阅读 151评论 0 0
  • 昨晚,儿子十分高兴的问我,这次我的申论写的如何?语气中透着满满的自信,真让人高兴。要知道,一年前,他还是个...
    山茶聆听阅读 206评论 0 0
  • 中秋节啦!大家节日快乐哈! 趁着假期有时间我尝试去临圆形的画作,这一幅感觉相对简单。找到比例位置后就开始了。修图比...
    四姑娘学涂鸦阅读 396评论 2 2
  • 第一步 新建一个project,或者如果你已经有project的话,那就直接新建一个module.注意选择Java...
    Ariel_Tian阅读 1,457评论 0 4

友情链接更多精彩内容