//
// NibScrollView.swift
// Created by Jo on .
// 无线轮播
// 使用方式,在sb或者xib中拖入view,然后设置成NibScrollView
// 添加图片使用addImages
//点击图片的回调:NibScrollViewDelegate
import UIKit
@objc protocol NibScrollViewDelegate: NSObjectProtocol {
//点击图片
@objc optional func scrollView(didTouchImageTag tag: Int)
}
extension NibScrollView: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
pageController.currentPage = Int(scrollView.contentOffset.x / bounds.width)
if sv_content.contentSize.width - Screen_Size.width <= sv_content.contentOffset.x {
pageController.currentPage = 0
sv_content.setContentOffset(CGPoint.init(x: 0, y: 0), animated: false)
}
}
//手动滚动的动画结束
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
scrollViewEndScroll()
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if scrollView.isTracking && !scrollView.isDragging && !scrollView.isDecelerating {
scrollViewEndScroll()
}
}
//自动滚动的动画结束触发
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
if sv_content.contentSize.width - Screen_Size.width <= sv_content.contentOffset.x {
pageController.currentPage = 0
sv_content.setContentOffset(CGPoint.init(x: 0, y: 0), animated: false)
}
}
func scrollViewEndScroll() {
if sv_content.contentSize.width - Screen_Size.width <= sv_content.contentOffset.x {
pageController.currentPage = 0
sv_content.setContentOffset(CGPoint.init(x: 0, y: 0), animated: false)
}
}
}
class NibScrollView: UIView {
var delegate: NibScrollViewDelegate?
let sv_content = UIScrollView()
let pageController = UIPageControl()
var timer: DispatchSourceTimer?
override func awakeFromNib() {
super.awakeFromNib()
backgroundColor = UIColor.white
pageController.isHidden = true
sv_content.delegate = self
sv_content.isPagingEnabled = true
sv_content.showsHorizontalScrollIndicator = false
initUI()
}
private func initUI() {
sv_content.translatesAutoresizingMaskIntoConstraints = false
pageController.translatesAutoresizingMaskIntoConstraints = false
addSubview(sv_content)
addSubview(pageController)
addConstraint(NSLayoutConstraint.init(item: sv_content, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 0))
addConstraint(NSLayoutConstraint.init(item: sv_content, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0))
addConstraint(NSLayoutConstraint.init(item: sv_content, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 0))
addConstraint(NSLayoutConstraint.init(item: sv_content, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1, constant: 0))
addConstraint(NSLayoutConstraint.init(item: pageController, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
addConstraint(NSLayoutConstraint.init(item: pageController, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0, constant: 100))
addConstraint(NSLayoutConstraint.init(item: pageController, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0, constant: 30))
addConstraint(NSLayoutConstraint.init(item: pageController, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: -15))
sv_content.contentSize = CGSize.init(width: 0, height: 0)
}
func removeAllBanner() {
for subView in sv_content.subviews {
subView.removeFromSuperview()
}
}
//imageHeight来控制图片高度(应和设置的NibScrollView高度一致)
func addImages(_ imagesPath: [String], imageHeight: CGFloat = 9.00 / 16.00 * Screen_Size.width) {
removeAllBanner()
var imageArray: [UIImageView] = []
for (index, path) in imagesPath.enumerated() {
// var viewLeading: UIView?
// if imageArray.count > 0 {
// viewLeading = imageArray[Int(index - 1)]
// }
let imageView = UIImageView.init(frame: CGRect.init(x:CGFloat(index) * Screen_Size.width, y: 0, width: Screen_Size.width, height: imageHeight))
sv_content.addSubview(imageView)
ImageManager.share.downloadImage(forView: imageView, withPath: path)
imageView.tag = index
imageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer.init(target: self, action: #selector(touchUpInsideByImage))
tap.numberOfTapsRequired = 1
tap.numberOfTapsRequired = 1
imageView.addGestureRecognizer(tap)
}
//在最后添加多一张(第一张图片)
if imagesPath.count > 1 {
let imageView = UIImageView.init(frame: CGRect.init(x:CGFloat(imagesPath.count) * Screen_Size.width, y: 0, width: Screen_Size.width, height: imageHeight))
sv_content.addSubview(imageView)
ImageManager.share.downloadImage(forView: imageView, withPath: imagesPath[0])
imageView.tag = 0
imageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer.init(target: self, action: #selector(touchUpInsideByImage))
tap.numberOfTapsRequired = 1
tap.numberOfTapsRequired = 1
imageView.addGestureRecognizer(tap)
}
pageController.currentPage = 0
pageController.numberOfPages = imagesPath.count
addTimer()
sv_content.contentSize = CGSize.init(width: bounds.width * CGFloat(imagesPath.count + 1), height: 0)
sv_content.delegate = self
}
@objc func touchUpInsideByImage( ges: UITapGestureRecognizer) {
if let imageView = ges.view {
if let delegate = delegate {
if delegate.responds(to: #selector(delegate.scrollView(didTouchImageTag:))) {
delegate.scrollView!(didTouchImageTag: imageView.tag)
}
}
}
}
//添加计时器用于自动轮播
private func addTimer() {
timer = DispatchSource.makeTimerSource(flags: DispatchSource.TimerFlags(rawValue: 0), queue: DispatchQueue.main)
//设置滑动时间
timer!.schedule(deadline: .now(), repeating: .seconds(3))
timer!.setEventHandler { [weak self] in
guard let strongSelf = self else {
return
}
strongSelf.scrollNextPage()
}
timer!.resume()
}
//滑动到下一页
fileprivate func scrollNextPage() {
var nextPage = pageController.currentPage + 1
// if nextPage > pageController.numberOfPages {
//
// nextPage = 0
// pageController.currentPage = 0
//
// }
sv_content.setContentOffset(CGPoint.init(x: CGFloat(nextPage) * sv_content.frame.width , y: 0), animated: true)
}
}
swift:无线轮播scrollerview
最新推荐文章于 2024-04-24 18:14:20 发布