[Swift]图片轮播

醉酒当歌 提交于 2020-03-07 19:51:52

体验了一些封装好的图片轮播demo
GitHub:https://github.com/Gamin-fzym/SomeCycleViewDemo

目录

SDCycleScrollView混编

ICycleView

LLCycleScrollView

SBCycleScrollView


SDCycleScrollView混编

GitHub:https://github.com/gsdios/SDCycleScrollView
cocoaPods

pod 'SDCycleScrollView'

import UIKit

class SDCycleScrollViewDeatails: UIViewController, SDCycleScrollViewDelegate {

    @IBOutlet weak var cycleScrollerView: SDCycleScrollView!
    var picModels: Array<Any>?;
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "SDCycleScrollViewDeatails";
        setupCycleScrollView();
    }

    func setupCycleScrollView() {
        var picMarr = Array<Any>();
        if let tempArr = picModels {
            for model in tempArr {
                picMarr.append((model as! GAPictureModel).path ?? "");
            }
        }
        
        cycleScrollerView.delegate = self;
        cycleScrollerView.bannerImageViewContentMode = .scaleToFill;
        // 图片数组
        cycleScrollerView.localizationImageNamesGroup = picMarr;
        // 缺省图
        cycleScrollerView.placeholderImage = UIImage(named: "pic_placeholder.jpg");
        // 标题数组
        //cycleScrollerView.titlesGroup = titMarr;
        // 自动滚动时间间隔
        cycleScrollerView.autoScrollTimeInterval = 3.0;
        // 翻页 右下角
        cycleScrollerView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        // 图片对应的标题的 背景色。(因为没有设标题)
        cycleScrollerView.titleLabelBackgroundColor = UIColor.clear;
        // 当前原点颜色
        cycleScrollerView.currentPageDotColor = UIColor.white;
        cycleScrollerView.pageDotColor = UIColor.lightGray;
        // 清除缓存
        SDCycleScrollView.clearImagesCache();
    }
    
    // MARK:SDCycleScrollViewDelegate
    
    func cycleScrollView(_ cycleScrollView: SDCycleScrollView!, didSelectItemAt index: Int) {
        print("1");
    }
    
}

ICycleView

GitHub:https://github.com/ixialuo/ICycleView
cocoaPods

pod 'ICycleView'

导入后运行报错:

Compiling for iOS 8.0, but module 'Kingfisher' has a minimum deployment target of iOS 10.0: /Users/gamin/Library/Developer/Xcode/DerivedData/SomeCycleViewDemo-bfgoyzmduevrsagkpcqtdejmhexs/Build/Products/Debug-iphonesimulator/Kingfisher/Kingfisher.framework/Modules/Kingfisher.swiftmodule/x86_64-apple-ios-simulator.swiftmodule

所以直接选择了手动导入ICycleView,不过在之前用cocoaPods导入Kingfisher。

pod 'Kingfisher'

import UIKit

class ICycleViewDeatails: UIViewController, ICycleViewDelegate {

    var picModels: Array<Any>?;

    override func viewDidLoad() {
        super.viewDidLoad()
        setupICycleView();
        
    }
    
    let scaleForPlus: CGFloat = 1.0;
    
    func setupICycleView() {
        var picMarr = Array<Any>();
        if let tempArr = picModels {
            for model in tempArr {
                picMarr.append((model as! GAPictureModel).path ?? "");
            }
        }
        // 图片赋值
        defaultCycleView.pictures = picMarr as! [String];
        // pageControlStyle属性必须在设置 pictures 后赋值,因为指示器是根据 numberOfPages 计算Size的
        defaultCycleView.pageControlStyle = .bottom(bottom: 10);
        //defaultCycleView.pageControlStyle = .right(trailing: 20*scaleForPlus);
    }
    
    // 惰性初始化滚动视图
    lazy var defaultCycleView: ICycleView = {
        let cycleView = ICycleView(frame: CGRect(x: 0, y: 50, width: UIScreen.main.bounds.width, height: 220));
        cycleView.delegate = self;
        cycleView.pageIndicatorTintColor = .lightGray;
        cycleView.currentPageIndicatorTintColor = .blue;
        cycleView.imgViewWidth = UIScreen.main.bounds.size.width*scaleForPlus;
        cycleView.placeholderImage = UIImage(named: "placehoder.jpg");
        view.addSubview(cycleView);
        return cycleView;
    }()

    // MARK: ICycleViewDelegate
    // 图片点击
    func iCycleView(cycleView: ICycleView, didSelectItemAt index: Int) {
        print("你点击了第 \(index) 张图片")
    }

    // 图片自动滚动
    func iCycleView(cycleView: ICycleView, autoScrollingItemAt index: Int) {
        print("当前滚动的图片是第 \(index) 张")
    }

    // 自定义Cell
    /*
    func iCycleView(cycleView: ICycleView, collectionView: UICollectionView, cellForItemAt indexPath: IndexPath, picture: String) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCycleViewCell
        cell.imgView.kf.setImage(with: URL(string: picture))
        cell.titleLab.text = "自定义Cell\n第 \(indexPath.item) 张图片"
        return cell
    }
   */
}

LLCycleScrollView

GitHub:https://github.com/LvJianfeng/LLCycleScrollView
cocoaPods

pod 'LLCycleScrollView'

import UIKit

class LLCycleScrollViewDeatails: UIViewController {

    var picModels: Array<Any>?;

    override func viewDidLoad() {
        super.viewDidLoad()
        setupLLCycleScrollView();
    }

    func setupLLCycleScrollView() {
        var picMarr = Array<Any>();
        if let tempArr = picModels {
            for model in tempArr {
                picMarr.append((model as! GAPictureModel).path ?? "");
            }
        }

        let bannerDemo = LLCycleScrollView.llCycleScrollViewWithFrame(CGRect.init(x: 0, y: 64, width: UIScreen.main.bounds.width, height: 220));
        // 是否自动滚动
        bannerDemo.autoScroll = true;

        // 是否无限循环,此属性修改了就不存在轮播的意义了
        bannerDemo.infiniteLoop = true;

        // 滚动间隔时间(默认为2秒)
        bannerDemo.autoScrollTimeInterval = 3.0;

        // 等待数据状态显示的占位图
        bannerDemo.placeHolderImage = UIImage(named: "pic_waiting.jpg");

        // 如果没有数据的时候,使用的封面图
        bannerDemo.coverImage = UIImage(named: "pic_placeholder.jpg");

        // 设置图片显示方式=UIImageView的ContentMode
        bannerDemo.imageViewContentMode = .scaleToFill;

        // 设置滚动方向( vertical || horizontal )
        bannerDemo.scrollDirection = .horizontal;

        // 设置当前PageControl的样式 (.none, .system, .fill, .pill, .snake)
        bannerDemo.customPageControlStyle = .snake;

        // 非.system的状态下,设置PageControl的tintColor
        bannerDemo.customPageControlInActiveTintColor = UIColor.red;

        // 设置.system系统的UIPageControl当前显示的颜色
        bannerDemo.pageControlCurrentPageColor = UIColor.white;

        // 非.system的状态下,设置PageControl的间距(默认为8.0)
        bannerDemo.customPageControlIndicatorPadding = 10.0;
        
        //设置PageControl的距底部的距离(默认为11.0)
        bannerDemo.pageControlBottom = 15.0;

        // 设置PageControl的位置 (.left, .right 默认为.center)
        bannerDemo.pageControlPosition = .center;

        // 背景色
        bannerDemo.collectionViewBackgroundColor = .lightGray;

        // 添加到view
        view.addSubview(bannerDemo);

        // 模拟网络图片获取
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .seconds(2)) {
            bannerDemo.imagePaths = picMarr as! Array<String>;
        }
    }

}

SBCycleScrollView

GitHub:https://github.com/xumaohuai/SBCycleScrollView
cocoaPods

pod 'SBCycleScrollView'

import UIKit

class SBCycleScrollViewDeatails: UIViewController, CycleScrollViewDelegate {

    var picModels: Array<Any>?;

    override func viewDidLoad() {
        super.viewDidLoad()
        setupSBCycleScrollView();
    }

    func setupSBCycleScrollView() {
        var picMarr = Array<Any>();
        if let tempArr = picModels {
            for model in tempArr {
                picMarr.append((model as! GAPictureModel).path ?? "");
            }
        }

        // 配置
        var options = CycleOptions();
        options.currentPageDotColor = .blue;
        options.radius = 10;
        options.pageStyle = PageControlStyle.jalapeno;
        
        // 初始化
        let frame = CGRect(x: 0, y: 64, width: UIScreen.main.bounds.width, height: 220);
        // 推荐方式,通过代理和占位图初始化,常用于图片异步获取的时候
        let cycleScrollView = CycleScrollView.initScrollView(frame: frame, delegate: self, placehoder: UIImage(named: "pic_placeholder.jpg"), cycleOptions: options);
        cycleScrollView.imageURLStringsGroup = picMarr as! [String];
        view.addSubview(cycleScrollView);
    }
    
    // MARK:SBCycleScrollViewDelegate
    
    func didSelectedCycleScrollView(_ cycleScrollView : CycleScrollView, _ Index : NSInteger) {
        print("点击了第\(Index)张图片")
    }
    
}

 



 

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!