加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 07:05:32 所属栏目:百科 来源:网络整理
导读:AutoScrollImagesView.swift //// AutoScrollImagesView .swift // SwiftRealmDatabaseTest//// Created by Sintn on 16 / 4 / 12. // Copyright ? 2016 年 sin. All rights reserved.//import UIKitclass AutoScrollImagesView: UIView,UIScrollViewDelegat

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 } */

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读