This repository has been archived by the owner on Aug 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a ReactiveScrollViewDelegate and replace usage of the MotionRunti…
…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
Showing
2 changed files
with
74 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |