@@ -19,7 +19,7 @@ import MaterialMotion
1919
2020class CarouselExampleViewController : ExampleViewController , UIScrollViewDelegate {
2121
22- var runtime : MotionRuntime !
22+ let delegate = ReactiveScrollViewDelegate ( )
2323 override func viewDidLoad( ) {
2424 super. viewDidLoad ( )
2525
@@ -29,10 +29,10 @@ class CarouselExampleViewController: ExampleViewController, UIScrollViewDelegate
2929 scrollView. autoresizingMask = [ . flexibleWidth, . flexibleHeight]
3030 scrollView. isPagingEnabled = true
3131 scrollView. contentSize = . init( width: view. bounds. size. width * 3 , height: view. bounds. size. height)
32- scrollView. delegate = self
32+ scrollView. delegate = delegate
3333 view. addSubview ( scrollView)
3434
35- pager = UIPageControl ( )
35+ let pager = UIPageControl ( )
3636 let size = pager. sizeThatFits ( view. bounds. size)
3737 pager. autoresizingMask = [ . flexibleWidth, . flexibleTopMargin]
3838 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
4545 ( title: " Page 3 " , description: " Page 3 description " , color: . secondaryColor) ,
4646 ]
4747
48- runtime = MotionRuntime ( containerView: view)
49-
50- let stream = runtime. get ( scrollView)
5148 for (index, data) in datas. enumerated ( ) {
5249 let page = CarouselPage ( frame: view. bounds)
5350 page. frame. origin. x = CGFloat ( index) * view. bounds. width
@@ -56,21 +53,19 @@ class CarouselExampleViewController: ExampleViewController, UIScrollViewDelegate
5653 page. iconView. backgroundColor = data. color
5754 scrollView. addSubview ( page)
5855
59- let pageEdge = stream . x ( ) . offset ( by: - page. frame. origin. x)
56+ let pageEdge = delegate . x ( ) . offset ( by: - page. frame. origin. x)
6057
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+ }
6764 }
68- }
6965
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+ }
7469 }
7570
7671 override func exampleInformation( ) -> ExampleInfo {
0 commit comments