@@ -30,26 +30,85 @@ class CycleBannerTestViewController: UIViewController, CycleBannerViewDelegate,
30
30
31
31
self . view. backgroundColor = UIColor . white
32
32
33
- cycleBannerView = CycleBannerView ( frame: . zero )
33
+ cycleBannerView = CycleBannerView ( frame: self . view . bounds )
34
34
cycleBannerView. delegate = self
35
35
cycleBannerView. itemSize = CGSize ( width: 300 , height: 180 )
36
- cycleBannerView. itemHeaderWidth = 10
37
- cycleBannerView. itemFooterWidth = 10
36
+ cycleBannerView. itemHeaderWidth = 0
37
+ cycleBannerView. itemFooterWidth = 0
38
38
cycleBannerView. scrollView. backgroundColor = UIColor . gray
39
39
cycleBannerView. translatesAutoresizingMaskIntoConstraints = false
40
40
self . view. addSubview ( cycleBannerView)
41
41
42
42
let bannerTop = NSLayoutConstraint ( item: cycleBannerView, attribute: . top, relatedBy: . equal, toItem: self . view, attribute: . top, multiplier: 1 , constant: 100 )
43
43
let bannerLeft = NSLayoutConstraint ( item: cycleBannerView, attribute: . left, relatedBy: . equal, toItem: self . view, attribute: . left, multiplier: 1 , constant: 30 )
44
- let bannerHeight = NSLayoutConstraint ( item: cycleBannerView, attribute: . height, relatedBy: . equal, toItem: nil , attribute: . notAnAttribute, multiplier: 1 , constant: 200 )
44
+ let bannerHeight = NSLayoutConstraint ( item: cycleBannerView, attribute: . height, relatedBy: . equal, toItem: nil , attribute: . notAnAttribute, multiplier: 1 , constant: 300 )
45
45
let bannerRight = NSLayoutConstraint ( item: cycleBannerView, attribute: . right, relatedBy: . equal, toItem: self . view, attribute: . right, multiplier: 1 , constant: - 30 )
46
46
self . view. addConstraints ( [ bannerTop, bannerLeft, bannerHeight, bannerRight] )
47
47
48
+ button1Tapped ( )
49
+ cycleBannerView. setCurrentIndex ( 1 , animated: false )
50
+
48
51
let view = UIView ( frame: CGRect ( x: 170 , y: 310 , width: 20 , height: 20 ) )
49
52
view. backgroundColor = UIColor . green
50
53
self . view. addSubview ( view)
51
54
52
- let item1 = CycleBannerViewItem ( )
55
+ let button = UIButton ( frame: CGRect ( x: 300 , y: 500 , width: 80 , height: 50 ) )
56
+ button. backgroundColor = UIColor . red
57
+ button. addTarget ( self , action: #selector( buttonTapped) , for: . touchUpInside)
58
+ self . view. addSubview ( button)
59
+
60
+ let button1 = UIButton ( frame: CGRect ( x: 300 , y: 560 , width: 80 , height: 50 ) )
61
+ button1. backgroundColor = UIColor . blue
62
+ button1. addTarget ( self , action: #selector( button1Tapped) , for: . touchUpInside)
63
+ self . view. addSubview ( button1)
64
+ }
65
+
66
+ override func viewDidLoad( ) {
67
+ super. viewDidLoad ( )
68
+
69
+ }
70
+
71
+ func cycleBannerView( _ cycleBannerView: CycleBannerView , didSelectItemAt index: Int ) {
72
+
73
+ print ( " tap item at = %d " , index)
74
+ }
75
+
76
+ func cycleBannerViewDidLayoutItems( _ cycleBannerView: CycleBannerView ) {
77
+
78
+ let scrollView = cycleBannerView. scrollView!
79
+ let contentOffset = scrollView. contentOffset
80
+ let contentCenterX = scrollView. frame. width / 2.0 + contentOffset. x
81
+ let totalWidth = scrollView. contentSize. width > 0 ? scrollView. contentSize. width : scrollView. frame. width
82
+ for item in cycleBannerView. items {
83
+ let scale = 1 - abs( contentCenterX - item. center. x) * 1.5 / totalWidth
84
+ item. transform = CGAffineTransform ( scaleX: scale, y: scale)
85
+ }
86
+ }
87
+
88
+ public func scrollViewDidScroll( _ scrollView: UIScrollView ) {
89
+
90
+ let contentOffset = scrollView. contentOffset
91
+ let contentCenterX = scrollView. frame. width / 2.0 + contentOffset. x
92
+ let totalWidth = scrollView. contentSize. width > 0 ? scrollView. contentSize. width : scrollView. frame. width
93
+ for item in cycleBannerView. items {
94
+ let scale = 1 - abs( contentCenterX - item. center. x) * 1.5 / totalWidth
95
+ item. transform = CGAffineTransform ( scaleX: scale, y: scale)
96
+ }
97
+ }
98
+
99
+ func buttonTapped( ) {
100
+ var index = cycleBannerView. currentIndex + 2
101
+ if index > cycleBannerView. numberOfItems {
102
+
103
+ index = 0
104
+ }
105
+ cycleBannerView. setCurrentIndex ( index, animated: true )
106
+ }
107
+
108
+ func button1Tapped( ) {
109
+
110
+
111
+ let item1 = CycleBannerViewItem ( frame: self . view. bounds)
53
112
item1. contentView. backgroundColor = UIColor . green
54
113
let item2 = CycleBannerViewItem ( )
55
114
item2. contentView. backgroundColor = UIColor . blue
@@ -92,15 +151,4 @@ class CycleBannerTestViewController: UIViewController, CycleBannerViewDelegate,
92
151
93
152
cycleBannerView. items = [ item1, item2, item3, item4, item5, item6, item7, item8, item9, item10, item11, item12, item13, item14, item15, item16, item17, item18, item19, item20]
94
153
}
95
-
96
- override func viewDidLoad( ) {
97
- super. viewDidLoad ( )
98
-
99
- }
100
-
101
- func cycleBannerView( _ cycleBannerView: CycleBannerView , didSelectItemAt index: Int ) {
102
-
103
- print ( " tap item at = %d " , index)
104
- }
105
-
106
154
}
0 commit comments