• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

swift自定义图片轮播视图

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Swift封装图片轮播视图:

import UIKit

class XHAdLoopView: UIView {

    private var pageControl : UIPageControl?
    private var imageScrollView : UIScrollView?
    private var currentPage: NSInteger?
    
    /*******************  重写get方法 **************/
    private var currentImgs = NSMutableArray()
    private var currentImages :NSMutableArray? {
        get{
            currentImgs.removeAllObjects()
            let count = self.images!.count
            var i = NSInteger(self.currentPage!+count-1)%count
            currentImgs.addObject(self.images![i])
            currentImgs.addObject(self.images![self.currentPage!])
            i = NSInteger(self.currentPage!+1)%count
            currentImgs.addObject(self.images![i])
            return currentImgs
        }
    }
    /************************************************/
    
    private var images: NSArray?
    private var autoPlay : Bool?
    private var isFromNet : Bool?
    private var delay : NSTimeInterval?
    
    var delegate:XHAdLoopViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    convenience init(frame:CGRect ,images:NSArray, autoPlay:Bool, delay:NSTimeInterval, isFromNet:Bool){
        self.init(frame: frame)
        self.images = images;
        self.autoPlay = autoPlay
        self.isFromNet = isFromNet
        self.delay = delay
        self.currentPage = 0
        
        createImageScrollView()
        createPageView()
        
        if self.autoPlay == true {
            startAutoPlay()
        }
    }

    //创建图片滚动视图
    private func createImageScrollView(){
        if images?.count == 0 {
            return
        }
        imageScrollView = UIScrollView(frame: self.bounds)
        imageScrollView?.showsHorizontalScrollIndicator = false
        imageScrollView?.showsVerticalScrollIndicator=false
        imageScrollView?.bounces = false
        imageScrollView?.delegate = self
        imageScrollView?.contentSize = CGSizeMake(self.bounds.width*3, 0)
        imageScrollView?.contentOffset = CGPointMake(self.frame.width, 0)
        imageScrollView?.pagingEnabled = true
        self.addSubview(imageScrollView!)
        
        for index in 0..<3 {
            let imageView = UIImageView(frame: CGRectMake(self.bounds.width*CGFloat(index), 0, self.bounds.width, self.bounds.height))
            imageView.userInteractionEnabled = true
            imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "imageViewClick"))
            
            
            if self.isFromNet == true {
                
            }
            else{
                imageView.image = UIImage(named: self.currentImages![index] as! String);
            }
            imageScrollView?.addSubview(imageView)
        }
        
    }
    
    //创建pageControl视图
    private func createPageView(){
        if images?.count == 0 {
            return
        }
        let pageW: CGFloat = 80
        let pageH: CGFloat = 25
        let pageX: CGFloat = self.bounds.width - pageW
        let pageY: CGFloat = self.bounds.height - pageH
        pageControl = UIPageControl(frame: CGRectMake(pageX, pageY, pageW, pageH))
        pageControl?.numberOfPages = images!.count
        pageControl?.currentPage = 0
        pageControl?.userInteractionEnabled = false
        self.addSubview(pageControl!)
        
    }
    
    private func startAutoPlay() {
        self.performSelector("nextPage", withObject: nil, afterDelay: delay!)
    }
    
    func nextPage() {
        NSObject.cancelPreviousPerformRequestsWithTarget(self, selector: "nextPage", object: nil)
        imageScrollView!.setContentOffset(CGPointMake(2 * self.frame.width, 0), animated: true)
        self.performSelector("nextPage", withObject: nil, afterDelay: delay!)
    }
    
    //每次图片滚动时刷新图片
    private func refreshImages(){
        for i in 0..<imageScrollView!.subviews.count {
            let imageView = imageScrollView!.subviews[i] as! UIImageView
            if self.isFromNet == true {
                
            }
            else{
                imageView.image = UIImage(named: self.currentImages![i] as! String);
            }
        }
        
        imageScrollView!.contentOffset = CGPointMake(self.frame.width, 0)
    }
    
    //图片点击
    func imageViewClick(){
        if self.delegate != nil && (self.delegate?.respondsToSelector("adLoopView:IconClick:")) != nil {
            self.delegate!.adLoopView(self, IconClick: currentPage!)
        }
    }

}

extension XHAdLoopView : UIScrollViewDelegate{
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        scrollView.setContentOffset(CGPointMake(self.frame.width, 0), animated: true)
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        let x = scrollView.contentOffset.x
        let width = self.frame.width
        if x >= 2*width {
            currentPage = (currentPage!+1) % self.images!.count
            pageControl!.currentPage = currentPage!
            refreshImages()
        }
        if x <= 0 {
            currentPage = (currentPage!+self.images!.count-1) % self.images!.count
            pageControl!.currentPage = currentPage!
            refreshImages()
        }
    }
}

//自定义代理方法
protocol XHAdLoopViewDelegate:NSObjectProtocol {
    func adLoopView(adLoopView:XHAdLoopView ,IconClick index:NSInteger)
}

调用封装的类:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let frame = CGRectMake(0, 20, view.bounds.width, view.bounds.width*0.6)
        let imageView = ["2.jpg","3.jpg","4.jpg"]
        
        let loopView = XHAdLoopView(frame: frame, images: imageView, autoPlay: true, delay: 2.5, isFromNet: false)
        loopView.delegate = self
        
        view.addSubview(loopView)
        
        
    }
    
}

//遵循协议代理,调用代理方法
extension ViewController : XHAdLoopViewDelegate {
    func adLoopView(adLoopView: XHAdLoopView, IconClick index: NSInteger) {
        print(index)
    }
}

效果图:

刚接触Swift,菜鸟一枚,望大神指点!由于懒得导入SDWebImage,所以图片来自网络时的加载省略了:

if self.isFromNet == true {

      // 加载网络图片         

}

Demo下载地址:https://github.com/huahua0809/XHAdScrollView


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift开发笔记2.浅谈闭包发布时间:2022-07-13
下一篇:
swift学习之一致性hash发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap