轮播图
1,往工程梨添加几张图片
import UIKit
class ViewController: UIViewController,UIScrollViewDelegate {
var scrollerView : UIScrollView! = nil
var pagec:UIPageControl! = nil
var timer:Timer! = nil
- 1.在viewDidload.swift先定义滑动条
- 2.往工程添加几张图片
- 3.选中滑块,几个手指滑和滑动多少下。添加到视图上。
- 4.定义一个timer,触发点击事件,添加一个方法。在方法里面获取当前的偏移量,如果到第四个要自动跳到第一个
- 5.timer的方法,开始拖拽的方法,结束拖拽的方法,滚动条的方法,协议的方法,滑动会执行的方法
- 6//根据偏移量定位到第几个点
import UIKit
class ViewController: UIViewController,UIScrollViewDelegate {
var scrollerView : UIScrollView! = nil
var pagec:UIPageControl! = nil
var timer:Timer! = nil
override func viewDidLoad() {
super.viewDidLoad()
let src = UIScrollView(frame: CGRect(x: 0, y: 100, width: self.view.frame.size.width, height: 300))
self.view.addSubview(src)
src.contentSize = CGSize(width: self.view.frame.size.width * 5, height: 300)
//加图片
for index in 1...5{
let name = "\(index).jpg"
let image = UIImage(named: name)
let x = CGFloat(index-1)*self.view.frame.size.width
let imageV = UIImageView(frame: CGRect(x: x, y: 0, width: self.view.frame.size.width, height: 300.0))
scrollerView = src
src.delegate = self
imageV.image = image
src.addSubview(imageV)
}
//UIPageControl
let page = UIPageControl(frame: CGRect(x: 100, y: 250, width: self.view.frame.size.width-200, height: 280))
self.pagec = page
page.numberOfPages = 4
page.currentPage = 0
page.addTarget(self, action: #selector(pageAction), for: .valueChanged)
// 没选中滑块的颜色
page.pageIndicatorTintColor = UIColor.red
//选中滑块的颜色
page.currentPageIndicatorTintColor = UIColor.green
self.view.addSubview(page)
//timer
self.timer = Timer.scheduledTimer(timeInterval:1, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
self.timer.fire()
}
func timerAction(){
//获取到当前的offest
let offset = self.scrollerView.contentOffset
let width = self.view.frame.size.width
// scrollerView进行滚动
scrollerView.setContentOffset(CGPoint(x:offset.x+width,y:offset.y), animated: true)
//让当前的offest.x+width,如果到第四个要跳到第一个
if scrollerView.contentOffset.x >= width * 4 {
let point = CGPoint(x: 0, y: 0)
scrollerView.contentOffset = point
}
}
//开始拖拽
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
self.timer.invalidate()
self.timer = nil
}
//结束拖拽
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
self.timer = Timer.scheduledTimer(timeInterval:1, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
//self.timer.fire()
//一秒之后再执行
let time = DispatchTime.now() + 1.0
DispatchQueue.main.asyncAfter(deadline:time){
if let _ = self.timer{
self.timer.fire()
}
}
}
func pageAction(page:UIPageControl){
let index = page.currentPage
let point = CGPoint(x:CGFloat(index) * self.view.frame.size.width, y: 0)
//修改偏移量
self.scrollerView.setContentOffset(point, animated: true)
}
//协议的方法,滑动会执行的方法
func scrollViewDidScroll(_ scrollView: UIScrollView) {
//获取到偏移量
let x = scrollerView.contentOffset.x
let width = self.view.frame.size.width
if (x >= width * 4) {
self.pagec.currentPage = 0
}else
{
self.pagec.currentPage = Int(x/width)
}
//根据偏移量定位到第几个点
// switch x {
// case 0:
// self.pagec.currentPage = 0
//
// case 1 * width:
// self.pagec.currentPage = 1
//
// case 2 * width:
// self.pagec.currentPage = 2
//
// case 3 * width:
// self.pagec.currentPage = 3
//
// default:
// print("other")
// }
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}