@@ -19,7 +19,7 @@ import MaterialMotion
19
19
20
20
class CarouselExampleViewController : ExampleViewController , UIScrollViewDelegate {
21
21
22
- var runtime : MotionRuntime !
22
+ let delegate = ReactiveScrollViewDelegate ( )
23
23
override func viewDidLoad( ) {
24
24
super. viewDidLoad ( )
25
25
@@ -29,10 +29,10 @@ class CarouselExampleViewController: ExampleViewController, UIScrollViewDelegate
29
29
scrollView. autoresizingMask = [ . flexibleWidth, . flexibleHeight]
30
30
scrollView. isPagingEnabled = true
31
31
scrollView. contentSize = . init( width: view. bounds. size. width * 3 , height: view. bounds. size. height)
32
- scrollView. delegate = self
32
+ scrollView. delegate = delegate
33
33
view. addSubview ( scrollView)
34
34
35
- pager = UIPageControl ( )
35
+ let pager = UIPageControl ( )
36
36
let size = pager. sizeThatFits ( view. bounds. size)
37
37
pager. autoresizingMask = [ . flexibleWidth, . flexibleTopMargin]
38
38
pager. frame = . init( x: 0 , y: view. bounds. height - size. height - 20 , width: view. bounds. width, height: size. height)
@@ -45,9 +45,6 @@ class CarouselExampleViewController: ExampleViewController, UIScrollViewDelegate
45
45
( title: " Page 3 " , description: " Page 3 description " , color: . secondaryColor) ,
46
46
]
47
47
48
- runtime = MotionRuntime ( containerView: view)
49
-
50
- let stream = runtime. get ( scrollView)
51
48
for (index, data) in datas. enumerated ( ) {
52
49
let page = CarouselPage ( frame: view. bounds)
53
50
page. frame. origin. x = CGFloat ( index) * view. bounds. width
@@ -56,21 +53,19 @@ class CarouselExampleViewController: ExampleViewController, UIScrollViewDelegate
56
53
page. iconView. backgroundColor = data. color
57
54
scrollView. addSubview ( page)
58
55
59
- let pageEdge = stream . x ( ) . offset ( by: - page. frame. origin. x)
56
+ let pageEdge = delegate . x ( ) . offset ( by: - page. frame. origin. x)
60
57
61
- runtime . connect ( pageEdge. rewriteRange ( start: 0 , end: 128 ,
62
- destinationStart : 1 , destinationEnd : 0 ) ,
63
- to : runtime . get ( page ) . alpha )
64
- runtime . connect ( pageEdge. rewriteRange ( start: - view. bounds. width, end: 0 ,
65
- destinationStart : 0.5 , destinationEnd : 1.0 ) ,
66
- to : runtime . get ( page . layer ) . scale )
58
+ pageEdge. rewriteRange ( start: 0 , end: 128 , destinationStart : 1 , destinationEnd : 0 ) . subscribeToValue {
59
+ page . alpha = $0
60
+ }
61
+ pageEdge. rewriteRange ( start: - view. bounds. width, end: 0 , destinationStart : 0.5 , destinationEnd : 1.0 ) . subscribeToValue {
62
+ page . layer . transform = CATransform3DMakeScale ( $0 , $0 , 1 )
63
+ }
67
64
}
68
- }
69
65
70
- var pager : UIPageControl !
71
-
72
- func scrollViewDidScroll( _ scrollView: UIScrollView ) {
73
- pager. currentPage = Int ( ( scrollView. contentOffset. x + scrollView. bounds. width / 2 ) / scrollView. bounds. width)
66
+ delegate. x ( ) . offset ( by: scrollView. bounds. width / 2 ) . scaled ( by: 1 / scrollView. bounds. width) . subscribeToValue {
67
+ pager. currentPage = Int ( $0)
68
+ }
74
69
}
75
70
76
71
override func exampleInformation( ) -> ExampleInfo {
0 commit comments