Swift5学习之旅----自动轮播图(UIProgressView、UIPageControl)
如果能提供到帮助请点个赞再关闭吧?
GitHub还没上传,用着Dropbox先吧(可能要翻墙
https://www.dropbox.com/sh/bk21cuya5bjew8o/AACOW3orlgvkoX7_yj12DJcQa?dl=0
Dropbox 轮播图
- UIProgressView
相关代码
import UIKitclass MyProgressView: UIProgressView {
override init(frame: CGRect) {
super.init(frame: frame)self.progressViewStyle = .bar
// self.setProgress(0, animated: true)//进度条颜色self.progressTintColor = UIColor.redself.trackTintColor = UIColor.gray//更改进度条的高度self.transform = CGAffineTransform.init(translationX: 1, y: 5)}required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")}
}
- UIPageControl
import UIKit
class MyPageControl: UIPageControl {
override init(frame: CGRect) {
super.init(frame: frame)//不是当前页面的小点颜色self.pageIndicatorTintColor = UIColor.black//当前页面的小点颜色self.currentPageIndicatorTintColor = UIColor.red
// self.backgroundColor = UIColor.blue}required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")}
}
- 功能的实现
//
// ViewController.swift
// 轮播图
//
// Created by 小天才智能电脑 on 2021/1/28.
//import UIKitclass ViewController: UIViewController,UIScrollViewDelegate {
let kScreenWidth = UIScreen.main.bounds.size.widthlet KScreenHeight = UIScreen.main.bounds.size.heightvar scrollView = MyScrollView()var pageControl = MyPageControl()var progressView = MyProgressView()var myTimer:Timer?var mycurrentPage:Int = 0var time:Int = 0var courses = [["name":"first","pic":"1.jpg"],["name":"second","pic":"2.jpg"],["name":"third","pic":"3.jpg"]]override func viewDidLoad() {
super.viewDidLoad()// Do any additional setup after loading the view.addScrollView()addImage()addPageControl()mycurrentPage = 1//添加定时器setupTimer()addProgress()}func addScrollView() {
self.scrollView.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: KScreenHeight/2)self.scrollView.contentSize = CGSize(width: kScreenWidth * 3, height: KScreenHeight/2)self.scrollView.delegate = selfself.view.addSubview(scrollView)}func addProgress() {
progressView.frame = CGRect(x: 50, y: 600, width: self.view.bounds.width - 100, height: 80)self.view.addSubview(progressView)}func addImage() {
let size = self.scrollView.bounds.sizefor (seq,course) in courses.enumerated(){
let page = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: KScreenHeight/2))let imageView = UIImageView(image: UIImage(named: course["pic"]!))imageView.frame = page.framepage.addSubview(imageView)page.backgroundColor = UIColor.graylet lab = UILabel(frame: CGRect(x: 15, y: KScreenHeight/2 - 100, width: kScreenWidth - 30, height: 40))lab.textAlignment = .center
// lab.backgroundColor = UIColor.bluelab.text = course["name"]page.addSubview(lab)page.frame = CGRect(x: CGFloat(seq)*(size.width), y: 0, width: size.width, height: size.height)self.scrollView.addSubview(page)}}func addPageControl() {
self.pageControl.frame = CGRect(x: kScreenWidth/2 - 50, y: KScreenHeight/2 + 50, width: self.view.bounds.width, height: 50)self.pageControl.numberOfPages = courses.count
// self.pageControl.backgroundColor = UIColor.blueself.view.addSubview(pageControl)}func setupTimer() {
self.myTimer = Timer.init(timeInterval: 0.1, target: self, selector: #selector(timeAction), userInfo: nil, repeats: true)RunLoop.main.add(self.myTimer!, forMode: .default)}@objc func timeAction() {
time += 1if time == 30 {
time = 0self.pageControl.currentPage += mycurrentPageif self.pageControl.currentPage == 0 || self.pageControl.currentPage == courses.count - 1{
mycurrentPage = -mycurrentPage}self.scrollView.contentOffset = CGPoint(x: CGFloat(kScreenWidth) * CGFloat(pageControl.currentPage), y: 0)}self.progressView.setProgress(Float(time)/30, animated: true)}func closeTimer() {
self.myTimer?.invalidate()self.myTimer = nil}func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let page = Int(scrollView.contentOffset.x/(self.view.frame.size.width))self.pageControl.currentPage = page}func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
self.closeTimer()}func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
self.setupTimer()}func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
self.pageControl.currentPage = Int(scrollView.contentOffset.x/kScreenWidth)}}