Skip to content
This repository has been archived by the owner on Aug 13, 2021. It is now read-only.

Commit

Permalink
Add a ReactiveScrollViewDelegate and replace usage of the MotionRunti…
Browse files Browse the repository at this point in the history
…me in the carousel demo.

Reviewers: O2 Material Motion, O4 Material Apple platform reviewers, #material_motion, markwei

Reviewed By: O2 Material Motion, O4 Material Apple platform reviewers, #material_motion, markwei

Tags: #material_motion

Differential Revision: http://codereview.cc/D3137
  • Loading branch information
Jeff Verkoeyen committed May 1, 2017
1 parent ffc8f8c commit db2d5bc
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 18 deletions.
31 changes: 13 additions & 18 deletions examples/CarouselExample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import MaterialMotion

class CarouselExampleViewController: ExampleViewController, UIScrollViewDelegate {

var runtime: MotionRuntime!
let delegate = ReactiveScrollViewDelegate()
override func viewDidLoad() {
super.viewDidLoad()

Expand All @@ -29,10 +29,10 @@ class CarouselExampleViewController: ExampleViewController, UIScrollViewDelegate
scrollView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
scrollView.isPagingEnabled = true
scrollView.contentSize = .init(width: view.bounds.size.width * 3, height: view.bounds.size.height)
scrollView.delegate = self
scrollView.delegate = delegate
view.addSubview(scrollView)

pager = UIPageControl()
let pager = UIPageControl()
let size = pager.sizeThatFits(view.bounds.size)
pager.autoresizingMask = [.flexibleWidth, .flexibleTopMargin]
pager.frame = .init(x: 0, y: view.bounds.height - size.height - 20, width: view.bounds.width, height: size.height)
Expand All @@ -45,9 +45,6 @@ class CarouselExampleViewController: ExampleViewController, UIScrollViewDelegate
(title: "Page 3", description: "Page 3 description", color: .secondaryColor),
]

runtime = MotionRuntime(containerView: view)

let stream = runtime.get(scrollView)
for (index, data) in datas.enumerated() {
let page = CarouselPage(frame: view.bounds)
page.frame.origin.x = CGFloat(index) * view.bounds.width
Expand All @@ -56,21 +53,19 @@ class CarouselExampleViewController: ExampleViewController, UIScrollViewDelegate
page.iconView.backgroundColor = data.color
scrollView.addSubview(page)

let pageEdge = stream.x().offset(by: -page.frame.origin.x)
let pageEdge = delegate.x().offset(by: -page.frame.origin.x)

runtime.connect(pageEdge.rewriteRange(start: 0, end: 128,
destinationStart: 1, destinationEnd: 0),
to: runtime.get(page).alpha)
runtime.connect(pageEdge.rewriteRange(start: -view.bounds.width, end: 0,
destinationStart: 0.5, destinationEnd: 1.0),
to: runtime.get(page.layer).scale)
pageEdge.rewriteRange(start: 0, end: 128, destinationStart: 1, destinationEnd: 0).subscribeToValue {
page.alpha = $0
}
pageEdge.rewriteRange(start: -view.bounds.width, end: 0, destinationStart: 0.5, destinationEnd: 1.0).subscribeToValue {
Reactive(page.layer).scale.value = $0
}
}
}

var pager: UIPageControl!

func scrollViewDidScroll(_ scrollView: UIScrollView) {
pager.currentPage = Int((scrollView.contentOffset.x + scrollView.bounds.width / 2) / scrollView.bounds.width)
delegate.x().offset(by: scrollView.bounds.width / 2).scaled(by: 1 / scrollView.bounds.width).subscribeToValue {
pager.currentPage = Int($0)
}
}

override func exampleInformation() -> ExampleInfo {
Expand Down
61 changes: 61 additions & 0 deletions src/reactivetypes/ReactiveScrollViewDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2017-present The Material Motion Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import Foundation

/**
A UIScrollViewDelegate implementation that exposes observable streams for the scroll view delegate
events.

Supported events:

- scrollViewDidScroll:

The canonical stream will emit the contentOffset each time a scrollViewDidScroll event is received.
*/
public final class ReactiveScrollViewDelegate: NSObject, UIScrollViewDelegate, MotionObservableConvertible {
public override init() {
super.init()
}

// MARK: Canonical stream

public func asStream() -> MotionObservable<CGPoint> {
return didScroll._map { $0.contentOffset }
}

// MARK: Streams

public var didScroll: MotionObservable<UIScrollView> {
return MotionObservable { observer in
self.didScrollObservers.append(observer)
return {
if let index = self.didScrollObservers.index(where: { $0 === observer }) {
self.didScrollObservers.remove(at: index)
}
}
}
}

// MARK: UIScrollViewDelegate

public func scrollViewDidScroll(_ scrollView: UIScrollView) {
didScrollObservers.forEach { $0.next(scrollView) }
}
private var didScrollObservers: [MotionObserver<UIScrollView>] = []

public let metadata = Metadata("Scroll view delegate")
}

0 comments on commit db2d5bc

Please sign in to comment.