swift:无线轮播scrollerview

19 篇文章 0 订阅
//
//  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)
         
      
    }
}












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值