宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-07-05 0d8f5fc8a516bfd07e425909e4a4432600572ee7
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//
//  BannerView.swift
//  OKProject
//
//  Created by 无故事王国 on 2022/5/7.
//  Copyright © 2022 yangwang. All rights reserved.
//
 
import UIKit
import UIKit
 
class BannerView: UIView,UIScrollViewDelegate{
 
    //图⽚⽔平放置到scrollView上
    private var scrollView:UIScrollView = UIScrollView()
    //⼩圆点标识
    private var pageControl:UIPageControl = UIPageControl()
    private var imageViews:Array = Array<UIImageView>()
 
 
    //图⽚集合
    private var images:Array<String> = []
    private var type:ImageType?
 
 
    private var currIndex = 0
    private var clickBlock :(Int)->Void = {index in}
    private var changeClouse:((Int,Int)->Void)?
 
    private var timer:Timer?
 
    // 默认⾃动播放 设置为false只能⼿动滑动
    var isAuto = true
    // 轮播间隔时间 默认6秒可以⾃⼰修改
    var interval:Double = 5
 
    private var startOffsetX:CGFloat = 0
 
    override func layoutSubviews() {
        super.layoutSubviews()
        pageControl.center = CGPoint(x:width/2,y:height - CGFloat(15))
        scrollView.contentSize = CGSize(width:width * CGFloat(images.count +
                                                              2),height:height)
    }
 
    public func setImages(images:Array<String>,type:ImageType
                          = .Image,imageClickBlock:@escaping (Int) -> Void) {
        self.type = type
        self.images = images
        self.clickBlock = imageClickBlock
        self.initLayout()
    }
 
    public func changeAtIndex(_ clouse:@escaping (Int,Int)->Void){
        changeClouse = clouse
    }
 
    private func initLayout(){
        if(self.images.count == 0){
            return
        }
 
        if self.images.count == 1{
            scrollView.isScrollEnabled = false
            closeTimer()
        }
 
        width = self.bounds.width
        height = self.bounds.height
 
        scrollView.frame = self.bounds
        scrollView.contentSize = CGSize(width:width * CGFloat(images.count +
                                                              2),height:height)
        scrollView.contentOffset = CGPoint(x:width,y:0)
        scrollView.isUserInteractionEnabled = true
        scrollView.isPagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.delegate = self
        self.addSubview(scrollView)
 
        var image = UIImageView()
        image.frame = CGRect(x:0,y:0,width:width,height:height)
        image.contentMode = .scaleAspectFill
        image.maskToBounds = true
        image.isUserInteractionEnabled = true
        setImage(image: image, index: images.count - 1)
        scrollView.addSubview(image)
        for i in 1 ... images.count{
            let image = UIImageView()
            image.frame = CGRect(x:width *
                                 CGFloat(i),y:0,width:width,height:height)
            image.contentMode = .scaleAspectFill
            image.maskToBounds = true
            image.isUserInteractionEnabled = true
            scrollView.addSubview(image)
            setImage(image: image, index: i - 1)
            addTapGesWithImage(image: image)
        }
        image = UIImageView()
        image.frame = CGRect(x:width * CGFloat(images.count +
                                               1),y:0,width:width,height:height)
        image.contentMode = .scaleAspectFill
        image.maskToBounds = true
        image.isUserInteractionEnabled = true
        scrollView.addSubview(image)
        setImage(image: image, index: 0)
 
//        pageControl.center = CGPoint(x:width/2,y:height - CGFloat(15))
        pageControl.isEnabled = true
        pageControl.numberOfPages = images.count
        pageControl.currentPageIndicatorTintColor = .white
        pageControl.pageIndicatorTintColor = .gray.withAlphaComponent(0.5)
        pageControl.isUserInteractionEnabled = false
        self.addSubview(pageControl)
        pageControl.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(-20)
            make.width.equalTo(width/2)
            make.height.equalTo(height - 15)
            make.centerX.equalToSuperview()
        }
        if(isAuto){
            openTimer()
        }
        setCurrent(currIndex: 0)
    }
 
    private func setImage(image:UIImageView,index:Int){
        if(type == .Image){
            image.image = UIImage.init(named:images[index])
        }else{
            image.load(url: images[index])
            image.contentMode = .scaleAspectFill
            image.maskToBounds = true
        }
    }
 
    func setCurrent(currIndex:Int) {
        if(currIndex < 0){
            self.currIndex = images.count - 1
        }else{
            self.currIndex = currIndex
        }
        pageControl.currentPage = self.currIndex
        scrollView.setContentOffset(CGPoint(x:width * CGFloat(self.currIndex +
                                                              1),y:0), animated: true)
        changeClouse?(currIndex,images.count)
    }
 
    //给图⽚添加点击⼿势
    private func addTapGesWithImage(image:UIImageView) {
        let tap = UITapGestureRecognizer(target: self, action: #selector(tap(_:)))
        image.isUserInteractionEnabled = true //让控件可以触发交互事件
        image.contentMode = .scaleAspectFill
        // image.clipsToBounds = true //超出⽗控件的部分不显示
        image.addGestureRecognizer(tap)
    }
 
    //点击图⽚,调⽤block
    @objc func tap(_ ges:UITapGestureRecognizer) {
        clickBlock((ges.view?.tag)!)
    }
 
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
 
    }
 
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        startOffsetX = scrollView.contentOffset.x
        closeTimer()
    }
 
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate
                                  decelerate: Bool) {
    }
 
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        if(scrollView.contentOffset.x > startOffsetX){
            currIndex = (currIndex + 1) % images.count
        }else{
            currIndex = (currIndex - 1) % images.count
        }
        setCurrent(currIndex: currIndex)
        openTimer()
    }
 
    func openTimer(){
        if(isAuto){
            closeTimer()
            timer = Timer.scheduledTimer(timeInterval: interval, target: self, selector:
                                            #selector(startAutoScroll), userInfo: nil, repeats: true)
        }
    }
 
    func closeTimer(){
        if(timer != nil){
            timer?.invalidate()
            timer = nil
        }
    }
 
 
    @objc func startAutoScroll(){
        if(isDisplayInScreen()){
            setCurrent(currIndex: (currIndex + 1) % images.count)
        }
    }
 
    func isDisplayInScreen() -> Bool{
        if(self.window == nil){
            return false
        }
        return true
    }
 
}
enum ImageType{
    case Image //本地图⽚
    case URL //URL
}