开发----Swift自定义View实现 实现自动循环滚动

AutoScrollImagesView.swift

//
//  AutoScrollImagesView.swift
//  SwiftRealmDatabaseTest
//
//  Created by Sintn on 16/4/12.
//  Copyright © 2016年 sin. All rights reserved.
//

import UIKit


class AutoScrollImagesView: UIView,UIScrollViewDelegate{

    let DEFAULTPAGECONTROLHEIGHT=CGFloat(30.0);
    let DEFAULTPAGECONTROLINDICATIRTINTCOLOR=UIColor.orangeColor();

    var  urlsArrays = Set<String>();
    var  imagesArrays = Set <UIView> ();
    let  imageScrollView=UIScrollView();
    let  imagePageControl=UIPageControl();

    var pageControlPageSize = 0;

    var timsTaskManager=NSTimer();



    override func layoutSubviews() {
        imageScrollView.frame = CGRect.init(x: 0,y: 0,width: self.frame.size.width,height: self.frame.size.height);
        imageScrollView.delegate=self;
        imageScrollView.pagingEnabled=true;
        imageScrollView.showsVerticalScrollIndicator=false;
        imageScrollView.showsHorizontalScrollIndicator=false;
        imageScrollView.canCancelContentTouches=false;
        self.addSubview(imageScrollView);
        imagePageControl.frame=CGRect.init(x: 0,y: self.frame.size.height-DEFAULTPAGECONTROLHEIGHT,height: DEFAULTPAGECONTROLHEIGHT);
        imagePageControl.pageIndicatorTintColor=DEFAULTPAGECONTROLINDICATIRTINTCOLOR;
        self.addSubview(imagePageControl);
    }


    func reload(urls:NSArray) {

        pageControlPageSize=urls.count;
        if pageControlPageSize==0 {
            return;
        }
        if urlsArrays.count>0 {
            urlsArrays.removeAll();
        }
        imagePageControl.numberOfPages=pageControlPageSize;
        imageScrollView.contentSize=contentSizeOfScrollView();
        initScrollImages(urls);
    }

    func initScrollImages(urls:NSArray){
        if imagesArrays.count>0 {
            for imageView in imagesArrays {
                imageView.removeFromSuperview();
            }
            imagesArrays.removeAll();
        }

        for otems in urls {
            let scorllImageView = UIImageView.init(frame: CGRect.init(x: 0,height: self.frame.size.height));
            scorllImageView.sd_setImageWithURL(NSURL.init(string: otems as! String));
            imagesArrays.insert(scorllImageView);
        }
        var  x = CGFloat(0.0);
        for genre in imagesArrays {
            genre.frame = CGRectOffset(genre.frame,x,0);
            imageScrollView.addSubview(genre);
            x += self.frame.size.width;
        }

    }

    func contentSizeOfScrollView() -> CGSize {
        return CGSize.init(width: self.frame.size.width*CGFloat(pageControlPageSize),height: self.frame.size.height);
    }


    func scrollViewDidScroll(scrollView: UIScrollView)
    {
        let v=imageScrollView.contentOffset.x;
        let k=(imageScrollView.contentSize.width / CGFloat.init(pageControlPageSize));
        imagePageControl.currentPage = Int.init(v/k);
    }

    func beginAutoScroll(timerInterval:NSTimeInterval) {
        timsTaskManager=NSTimer.scheduledTimerWithTimeInterval(timerInterval,target: self,selector: #selector(AutoScrollImagesView.timerTask),userInfo: nil,repeats: true)
    }

    func timerTask() {
        let pt = imageScrollView.contentOffset;
        if pt.x==CGFloat.init(self.frame.size.width * CGFloat.init(pageControlPageSize-1)) {
            imageScrollView.contentOffset=CGPoint.init(x: 0,y: 0);
            imageScrollView.scrollRectToVisible(CGRect.init(x:self.frame.size.width,height: self.frame.size.height),animated: true);
        }else
        {
            imageScrollView.scrollRectToVisible(CGRect.init(x: pt.x+self.frame.size.width,animated: true);
        }

        if imagePageControl.currentPage<pageControlPageSize-1 {
            imagePageControl.currentPage=imagePageControl.currentPage+1;
        }else
        {
            imagePageControl.currentPage=0;
        }
    }
    func stopAutoScroll() {
        if timsTaskManager.valid {
            timsTaskManager.invalidate();
        }
    }
    override func delete(sender: AnyObject?) {
        super.delete(sender);
        stopAutoScroll();
    }
    /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. override func drawRect(rect: CGRect) { // Drawing code } */

}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...