无故事王国
2023-10-26 152da0e2d8cd3bd522fb04d1114f0930c33c9fdf
WanPai/Common/View/CommonBannerView.swift
@@ -35,14 +35,16 @@
    var interval:Double = 3
    private var startOffsetX:CGFloat = 0
   private var fillBounds:CGRect?
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    public func setImages(images:Array<String>,type:ImageType
                          = .Image,imageClickBlock:@escaping (Int) -> Void) {
                    = .Image,fillBounds:CGRect? = nil,imageClickBlock:@escaping (Int) -> Void) {
        self.type = type
      self.fillBounds = fillBounds
        self.images = images
        self.clickBlock = imageClickBlock
        self.initLayout()
@@ -53,16 +55,23 @@
            return
        }
        width = self.bounds.width
        height = self.bounds.height
      if fillBounds != nil{
         width = fillBounds!.width
         height = fillBounds!.height
         scrollView.frame = CGRect(origin: .zero, size: fillBounds!.size)
      }else{
         width = self.bounds.width
         height = self.bounds.height
         scrollView.frame = self.bounds
      }
        scrollView.frame = self.bounds
        scrollView.contentSize = CGSize(width:width * CGFloat(images.count +
                                                              2),height:height)
        scrollView.contentOffset = CGPoint(x:width,y:0)
        scrollView.isUserInteractionEnabled = true
        scrollView.isPagingEnabled = true
      scrollView.isScrollEnabled = images.count > 1
      scrollView.contentInsetAdjustmentBehavior = .never
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.delegate = self
        self.addSubview(scrollView)
@@ -122,15 +131,17 @@
        }
    }
    func setCurrent(currIndex:Int) {
   func setCurrent(currIndex:Int,animate:Bool = false) {
        if(currIndex < 0){
            self.currIndex = images.count - 1
        }else{
            self.currIndex = currIndex
        }
        pageControl.currentPage = self.currIndex
        scrollView.setContentOffset(CGPoint(x:width * CGFloat(self.currIndex +
                                                              1),y:0), animated: false)
      if animate{
         scrollView.setContentOffset(CGPoint(x:width * CGFloat(self.currIndex + 1),y:0), animated: false)
      }
    }
        //给图⽚添加点击⼿势
@@ -188,7 +199,7 @@
    @objc func startAutoScroll(){
        if(isDisplayInScreen()){
            setCurrent(currIndex: (currIndex + 1) % images.count)
            setCurrent(currIndex: (currIndex + 1) % images.count,animate: true)
        }
    }