当前位置:   article > 正文

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

swift5 guide 自动滚动

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, width: self.frame.size.width, 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, y: 0, width: self.frame.size.width, 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, y: 0, width: self.frame.size.width, height: self.frame.size.height), animated: true);
        }else
        {
            imageScrollView.scrollRectToVisible(CGRect.init(x: pt.x+self.frame.size.width, y: 0, width: self.frame.size.width, height: self.frame.size.height), 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
     }
     */

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/780935
推荐阅读
相关标签
  

闽ICP备14008679号