用Swift3写了一个图片轮播View

作者: GTMYang | 来源:发表于2016-12-01 09:49 被阅读141次
江山如画
先上代码 github代码地址
欢迎大家使用和提出建议,这是我分享的第一个代码,多多支持!

ImageCarouselView 是一个用Swift3实现的图片轮播控件

  • 不依赖任何第三方类库
  • 代码简洁,使用简单

Demo

alt tagalt tag

Requirements

  • iOS 7.0+
  • Xcode 8.0+
  • Swift 3.0+

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

CocoaPods 1.1.0+ is required to build ImageCarouselView.

To integrate SnapKit into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'ImageCarouselView'
end

Then, run the following command:

$ pod install

Manually

If you prefer not to use either of the aforementioned dependency managers, you can integrate ImageCarouselView into your project manually.


Usage

Quick Start

import ImageCarouselView

class MyViewController: UIViewController {

    lazy var imageCarouselView: ImageCarouselView = {
        let screenWidth = UIScreen.main.bounds.size.width
        let frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenWidth*320/568)

        let images = [["url": "http://pic1.zhimg.com/05a55004e42ef9d778d502c96bc198a4.jpg", "tit": "Whatever is worth doing is worth doing well."],
        ["url": "http://pic3.zhimg.com/cd1240013a1c68392c81ba2df54ebb52.jpg", "tit": "You may be out of my sight, but never out of my mind."],
        ["url": "http://pic1.zhimg.com/163af5e8c239e24654d26f3059cdccb4.jpg", "tit": "When the whole world is about to rain, let’s make it clear in our heart together."],
        ["url": "http://pic4.zhimg.com/fe48f0349c20a50a5ad1491e569920d3.jpg", "tit": "I’ll think of you every step of the way."],
        ["url": "http://pic2.zhimg.com/325aa7289be798e36518bdc6d18e3581.jpg", "tit": "Love is not a maybe thing. You know when you love someone."]]
        let carouselView = ImageCarouselView(frame: frame, imageArray: images)
        carouselView.delegate = self
        carouselView.isAutoScroll = true
        //carouselView.scrollInterval = 2
        //carouselView.isShowImageText = false

        return carouselView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.addSubview(self.imageCarouselView)
    }

}

extension ViewController: ImageCarouselViewDelegate {
        func imageCarouselView(_ imageCarouselView: ImageCarouselView, didSelectItemAt index:Int) {
            //self.toast(message: "touched the \(index) index image")
        }
        // 设置网络图的方法(这里将图片设置方法交给代理,避免本控件依赖网络库)
        func setImageForImageView(imageV: UIImageView, byUrlString: String?, placeholderImage:UIImage?){
            imageV.contentMode = UIViewContentMode.scaleAspectFill
            imageV.setImageWith(URL(string: byUrlString!)!, placeholderImage: placeholderImage)
        }
        // 取出图片数据里面的文本字段值
        func titleForData(_ imageData:Any) -> String {
            let data = imageData as? Dictionary<String, String>
            return data!["tit"]!
        }
        // 取出图片数据里面的图片Url字段值
        func imageUrlStringForData(_ imageData:Any) -> String {
            let data = imageData as? Dictionary<String, String>
            return data!["url"]!
        }
}

License

ImageCarouselView is released under the MIT license. See LICENSE for details.

相关文章

  • 用Swift3写了一个图片轮播View

    ImageCarouselView 是一个用Swift3实现的图片轮播控件 不依赖任何第三方类库 代码简洁,使用简...

  • ng-repeat不能正常轮播问题

    用ng+swiperjs写了一个轮播图,倒腾了一上午也轮播也没有实现,总是出现一张图片。查了好久资料才找到答案,故...

  • 用动画做图片轮播

    图片轮播的新方法,用动画实现轮播: 1.将需要轮播的图片用标签放在同一位置; 2.通过改变各个图片的透明度实现轮播...

  • View的onAttachedToWindow引发的图片轮播问题

    由View的onAttachedToWindow引发的图片轮播问题探究 前言 本篇文章是在View的postDel...

  • 轮播图优化-实现子view的复用

    轮播图控件刚毕业的同学们就会写了,这里简单介绍下自己在做RN轮播图时一些心得: 回收与复用弃用的子view,至多只...

  • 一个支持Fragment,View,图片轮播的Banner

    之前有一个项目中有用到轮播,不过不是简单的轮播图片就完了,而是要轮播很多个View,一开始我的想法和大家一样在gi...

  • vue图片无限轮播

    近期项目中出现了很多图片轮播的需求,趁此机会写了一个关于图片自动轮播,手动切换的小Demo,具体效果看下图: 具体...

  • 轮播图

    ios美女小同事写了一个可以支持显示一张图片,两张图片左右轮播,三张以及三张以上图片无限轮播的轮子,支持加载本地图...

  • 简单广告轮播demo

    思路: scrollview pageControl开启,利用三张图片实现轮播 ,始终显示中间view,通过左右滑...

  • ios垂直轮播label

    使用两个Label(也可以为view,可以在view上添加图片等,原理一样)实现垂直方向的广告轮播 广告对于一个A...

网友评论

本文标题:用Swift3写了一个图片轮播View

本文链接:https://www.haomeiwen.com/subject/jfrxmttx.html