@@ -15,7 +15,7 @@ import UIKit
15
15
@objc optional func cycleBannerViewDidLayoutItems( _ cycleBannerView: CycleBannerView )
16
16
}
17
17
18
- open class CycleBannerView : UIView , UIScrollViewDelegate , UIGestureRecognizerDelegate {
18
+ open class CycleBannerView : UIView , UIScrollViewDelegate {
19
19
20
20
@IBOutlet weak var delegate : CycleBannerViewDelegate ?
21
21
@@ -46,10 +46,7 @@ open class CycleBannerView: UIView, UIScrollViewDelegate, UIGestureRecognizerDel
46
46
}
47
47
}
48
48
}
49
- // // 所有显示的Items,
50
- // // 如果cycleScrolling=false,则此属性与items值相同
51
- // // 否则,此属性值比Items多两个,其中internalItems[0] == items.last,internalItems.last == items[0],中间的都一样
52
- // private var internalItems: [CycleBannerViewItem] = []
49
+
53
50
// items count
54
51
public var numberOfItems : Int {
55
52
@@ -65,26 +62,13 @@ open class CycleBannerView: UIView, UIScrollViewDelegate, UIGestureRecognizerDel
65
62
public var itemFooterWidth : CGFloat = 0
66
63
67
64
// 自动滚动,小于0表示不自动滚动,否则每次按时间滚动一页,默认为0
68
- public var autoplayDuration : Double = 0 {
69
- didSet {
70
- if autoplayDuration > 0
71
- && numberOfItems > 0 {
72
- startAutoplay ( )
73
- } else {
74
- stopAutoplay ( )
75
- }
76
- }
77
- }
65
+ public var autoplayDuration : Double = 0
78
66
79
67
// 是否循环滚动, 默认false
80
- public var cycleScrolling : Bool = false {
81
- didSet {
82
-
83
- }
84
- }
68
+ public var cycleScrolling : Bool = false
85
69
86
70
// select index
87
- private ( set) var currentIndex : Int = 0
71
+ fileprivate ( set) var currentIndex : Int = 0
88
72
89
73
public func setCurrentIndex( _ index: Int , animated: Bool ) {
90
74
if index < 0
@@ -101,10 +85,6 @@ open class CycleBannerView: UIView, UIScrollViewDelegate, UIGestureRecognizerDel
101
85
// scroll items
102
86
private func _scrollToCurrentIndexItem_( _ animated: Bool ) {
103
87
104
- // if currentIndex == 0 {
105
- //
106
- // scrollView.setContentOffset(.zero, animated: animated)
107
- // } else
108
88
if let item = item ( at: currentIndex) {
109
89
110
90
let offsetX = item. center. x - scrollView. frame. width / 2.0
@@ -228,7 +208,6 @@ open class CycleBannerView: UIView, UIScrollViewDelegate, UIGestureRecognizerDel
228
208
scrollView. addSubview ( item)
229
209
230
210
let tap = UITapGestureRecognizer ( target: self , action: #selector( _itemTapped_ ( sender: ) ) )
231
- tap. delegate = self
232
211
item. addGestureRecognizer ( tap)
233
212
}
234
213
}
@@ -253,14 +232,6 @@ open class CycleBannerView: UIView, UIScrollViewDelegate, UIGestureRecognizerDel
253
232
254
233
public func indexOfItem( at point: CGPoint ) -> Int ? {
255
234
256
- // // 第一个item的x
257
- // var enumertatedItems: [CycleBannerViewItem] = items
258
- // if (point.x < scrollView.contentSize.width - (scrollView.frame.width - itemSize.width) / 2.0 - itemFooterWidth
259
- // && point.x > scrollView.contentSize.width / 2.0)
260
- // || (point.x < (scrollView.frame.width - itemSize.width) / 2.0 - itemHeaderWidth){
261
- // // 从最后一个开始遍历
262
- // enumertatedItems = items.reversed()
263
- // }
264
235
for (index, item) in items. enumerated ( ) {
265
236
if point. x < item. frame. maxX + itemFooterWidth
266
237
&& point. x > item. frame. minX - itemHeaderWidth {
@@ -323,10 +294,10 @@ open class CycleBannerView: UIView, UIScrollViewDelegate, UIGestureRecognizerDel
323
294
delegate? . scrollViewDidScroll ? ( scrollView)
324
295
}
325
296
326
- // MARK: UIGestureRecognizerDelegate
327
- public func gestureRecognizer( _ gestureRecognizer: UIGestureRecognizer , shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer ) -> Bool {
328
- return true
329
- }
297
+ // // MARK: UIGestureRecognizerDelegate
298
+ // public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
299
+ // return true
300
+ // }
330
301
331
302
// event
332
303
@objc private func _itemTapped_( sender: UITapGestureRecognizer ? ) {
@@ -346,7 +317,8 @@ extension CycleBannerView {
346
317
// 开始之前,先停止之前的操作
347
318
stopAutoplay ( )
348
319
// 时间小于0,不自动滚动
349
- if autoplayDuration <= 0 {
320
+ if autoplayDuration <= 0
321
+ || numberOfItems == 0 {
350
322
return
351
323
}
352
324
perform ( #selector( autoplayBanner) ,
@@ -360,6 +332,14 @@ extension CycleBannerView {
360
332
object: nil )
361
333
}
362
334
335
+ public func pauseAutoplay( ) {
336
+ stopAutoplay ( )
337
+ }
338
+
339
+ public func resumeAutoplay( ) {
340
+ startAutoplay ( )
341
+ }
342
+
363
343
@objc fileprivate func autoplayBanner( ) {
364
344
var nextItemIndex = currentIndex + 1
365
345
if cycleScrolling
@@ -384,11 +364,11 @@ extension CycleBannerView {
384
364
385
365
let contentMinX = ( scrollView. frame. width - itemSize. width) / 2.0 - itemHeaderWidth
386
366
let contentMaxX = scrollView. contentSize. width - ( scrollView. frame. width - itemSize. width) / 2.0 + itemFooterWidth
387
- if scrollView. contentOffset. x < contentMinX {
367
+ if scrollView. contentOffset. x <= contentMinX {
388
368
// 滑到了最左边,把最右边的item挪到最左边
389
369
items [ 0 ] . center. x = scrollView. frame. width / 2.0
390
370
items [ numberOfItems - 1 ] . center. x = contentMinX - itemFooterWidth - itemSize. width / 2.0
391
- } else if scrollView. contentOffset. x > contentMaxX - scrollView. frame. width {
371
+ } else if scrollView. contentOffset. x >= contentMaxX - scrollView. frame. width {
392
372
// 滑到最右边,把最左边的item挪到最右边
393
373
items [ numberOfItems - 1 ] . center. x = scrollView. contentSize. width - scrollView. frame. width / 2.0
394
374
items [ 0 ] . center. x = contentMaxX + itemHeaderWidth + itemSize. width / 2.0
@@ -398,11 +378,15 @@ extension CycleBannerView {
398
378
}
399
379
400
380
if scrollView. contentOffset. x <= - itemSize. width - itemHeaderWidth - itemFooterWidth {
401
- // 循环滚动的时候,滑到最后一个item(此时最后一个item在第一个item的左边),则调回开头
381
+ // 循环滚动的时候,滑到最左边item,此时最左边item.index=numberOfItems-1,则跳回到numberOfItems-1本来的位置
402
382
scrollView. setContentOffset ( CGPoint ( x: scrollView. contentSize. width - scrollView. frame. width, y: 0 ) , animated: false )
383
+ currentIndex = numberOfItems - 1
384
+ pageControl. currentPage = currentIndex
403
385
} else if scrollView. contentOffset. x >= scrollView. contentSize. width - scrollView. frame. width + itemSize. width + itemFooterWidth + itemHeaderWidth {
404
- // 循环滚动的时候,滑到第一个item(此时第一个item在最后一个item的右边) ,则调回开头
386
+ // 循环滚动的时候,滑到最右边item,此时最右边Item.index=0 ,则调回开头
405
387
scrollView. setContentOffset ( . zero, animated: false )
388
+ currentIndex = 0
389
+ pageControl. currentPage = currentIndex
406
390
}
407
391
}
408
392
}
0 commit comments