zoukankan      html  css  js  c++  java
  • swift轮播图代码

    import UIKit

    private let CycleCellID = "CycleCellID"

     

    class BannerCycleView: UIView {

     

        var layout  : UICollectionViewFlowLayout!

        var collectionView :UICollectionView!

        var pageContol : UIPageControl?

        var cycleTimer : Timer?

      

        let timeInterval : TimeInterval = 2

        

        var cycleModels : [CycleModel]? {

            didSet {

                collectionView.reloadData()

                pageContol?.numberOfPages = cycleModels?.count ?? 0

                let indexPath = IndexPath(item: (cycleModels?.count ?? 0) * 100, section: 0)

                collectionView.scrollToItem(at: indexPath, at: .left, animated: false)

                removeCycleTimer()

                addCycleTimer()

            }

        }

        

        

        override init(frame: CGRect) {

            super.init(frame: frame)

            

                setupUI()

            

        }

        

        required init?(coder aDecoder: NSCoder) {

            fatalError("init(coder:) has not been implemented")

        }

     

    }

     

     

    extension BannerCycleView {

        func  setupUI() {

            

            self.layout = UICollectionViewFlowLayout()

            self.layout.itemSize = CGSize( kScreenW, height: 200)

            self.layout.minimumLineSpacing = 0

            self.layout.minimumInteritemSpacing = 0

            self.layout.scrollDirection = .horizontal

            self.collectionView = UICollectionView.init(frame: self.bounds, collectionViewLayout: self.layout)

            

            self.collectionView.isPagingEnabled = true

            self.collectionView.showsHorizontalScrollIndicator = false

            self.collectionView.delegate = self

            self.collectionView.dataSource = self

            self.collectionView .register(CollectionCycleCell.self, forCellWithReuseIdentifier: CycleCellID)

            self.collectionView.backgroundColor = UIColor.white

            self.addSubview(self.collectionView)

            

            self.pageContol = UIPageControl(frame: CGRect(x: self.collectionView.center.x - 50 , y: 130, 100, height: 20))

            self.addSubview(self.pageContol!)

            

        }

     

    }

     

     

    extension BannerCycleView : UICollectionViewDataSource {

     

        

        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

            return (cycleModels?.count ?? 0) * 10000

        }

       

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

            

            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CycleCellID, for: indexPath) as! CollectionCycleCell

                    

            print(cycleModels![indexPath.item % cycleModels!.count])

            cell.cycleModel = cycleModels![indexPath.item % cycleModels!.count]

            

            return cell

            

        }

     

    }

     

    extension BannerCycleView : UICollectionViewDelegate {

     

        func scrollViewDidScroll(_ scrollView: UIScrollView) {

          // 计算偏移量

         let offsetX = scrollView.contentOffset.x + scrollView.bounds.width * 0.5

         

          // 计算页码当前页

            pageContol?.currentPage = Int(offsetX / scrollView.bounds.width) % (cycleModels?.count ?? 1)

            

            

        }

        

        func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {

            removeCycleTimer()

        }

        

        func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

            addCycleTimer()

        }

     

    }

     

     

    extension BannerCycleView {

      

        fileprivate func addCycleTimer(){

        

            cycleTimer = Timer(timeInterval: 2.0, target: self, selector: #selector(self.scrollToNext), userInfo: nil, repeats: true)

            RunLoop.main.add(cycleTimer!, forMode: .commonModes)

            

        

        }

     

        fileprivate func removeCycleTimer(){

            cycleTimer?.invalidate()

            cycleTimer = nil

        }

        

        @objc fileprivate func scrollToNext() {

            collectionView.setContentOffset(CGPoint(x:collectionView.contentOffset.x + collectionView.bounds.width , y:0), animated: true)

            

        }

        

     

    }

     

    // ------ CollectionCycleCell

     

    import UIKit

     

    class CollectionCycleCell: UICollectionViewCell {

        

        var iconImageView: UIImageView!

        

        var titleStringLabel : UILabel!

        

     

        func setupUI() {

            self.backgroundColor = UIColor.white

            

            self.iconImageView = UIImageView()

            self.iconImageView.frame = CGRect(x: 0, y: 0, kScreenW, height: 200)

            

            self.titleStringLabel = UILabel()

            self.titleStringLabel.text = ""

            self.titleStringLabel.font = UIFont.systemFont(ofSize: 13)

            self.titleStringLabel.frame = CGRect(x:  0, y:  163, iconImageView.frame.width, height: 20)

            self.titleStringLabel.backgroundColor = UIColor.lightGray.withAlphaComponent(0.8)

            

            self.addSubview(iconImageView)

          

            self.iconImageView.addSubview(self.titleStringLabel)

            

        }

        

        

        

        override init(frame: CGRect) {

            super.init(frame: frame)

            

            setupUI()

            

        }

        

        required init?(coder aDecoder: NSCoder) {

            fatalError("init(coder:) has not been implemented")

        }

        

        // MARK: 定义模型属性

        var cycleModel : CycleModel? {

            didSet {

                titleStringLabel.text = cycleModel?.title

                let iconURL = URL(string: cycleModel?.bigimg ?? "")!

                iconImageView.kf.setImage(with: iconURL, placeholder: UIImage(named: "homepage_refresh_tv"))

            }

        }

     

    }

     

     

     

    //---- model

    import UIKit

     

    class CycleModel: NSObject {

        var title : String = ""

        var smallimg : String = ""

        var bigimg : String = ""

        

        init(dict : [String : Any]) {

            super.init()

            setValuesForKeys(dict)

        }

        

        override func setValue(_ value: Any?, forUndefinedKey key: String) {}

     

    }

     

    把埋怨的话放一放,把负能量收一收。太阳喜欢向日葵,人们也会喜欢上爱笑的你。习惯好的自己,一切都会好的。

  • 相关阅读:
    git commit --amend
    Interleaving String leetcode
    Longest Common Substring
    Distinct Subsequences Leetcode
    Longest Common Subsequence
    Palindrome Partitioning II Leetcode
    百度面试时遇到这样一个问题:给定数组a[];计算除最后一个元素之外其他元素的和,下面的代码有什么问题吗
    sizeof与strlen
    网络是怎么连接的(2)?
    网络是怎么连接的(1)?
  • 原文地址:https://www.cnblogs.com/supersr/p/5519251.html
Copyright © 2011-2022 走看看