TransformGestureRecognizers.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (c) RxSwiftCommunity
  2. // Permission is hereby granted, free of charge, to any person obtaining a copy
  3. // of this software and associated documentation files (the "Software"), to deal
  4. // in the Software without restriction, including without limitation the rights
  5. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  6. // copies of the Software, and to permit persons to whom the Software is
  7. // furnished to do so, subject to the following conditions:
  8. // The above copyright notice and this permission notice shall be included in
  9. // all copies or substantial portions of the Software.
  10. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  14. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  15. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  16. // THE SOFTWARE.
  17. import UIKit
  18. import RxSwift
  19. import RxCocoa
  20. public struct TransformGestureRecognizers {
  21. public let panGesture: UIPanGestureRecognizer
  22. public let rotationGesture: UIRotationGestureRecognizer
  23. public let pinchGesture: UIPinchGestureRecognizer
  24. }
  25. public struct TransformVelocity {
  26. let translation: CGPoint
  27. let rotation: CGFloat
  28. let scale: CGFloat
  29. }
  30. public typealias TransformConfiguration = Configuration<TransformGestureRecognizers>
  31. public typealias TransformControlEvent = ControlEvent<TransformGestureRecognizers>
  32. public typealias TransformObservable = Observable<TransformGestureRecognizers>
  33. extension Reactive where Base: View {
  34. public func transformGestures(
  35. configuration: TransformConfiguration? = nil
  36. ) -> TransformControlEvent {
  37. let source = Observable.combineLatest(panGesture(), rotationGesture(), pinchGesture()) {
  38. return TransformGestureRecognizers(
  39. panGesture: $0,
  40. rotationGesture: $1,
  41. pinchGesture: $2
  42. )
  43. }
  44. return ControlEvent(events: source)
  45. }
  46. }
  47. extension ObservableType where Element == TransformGestureRecognizers {
  48. public func when(_ states: GestureRecognizerState...) -> Observable<Element> {
  49. return filter { gestures in
  50. return states.contains(gestures.panGesture.state)
  51. || states.contains(gestures.rotationGesture.state)
  52. || states.contains(gestures.pinchGesture.state)
  53. }
  54. }
  55. public func asTransform(in view: TargetView = .view) -> Observable<(transform: CGAffineTransform, velocity: TransformVelocity)> {
  56. return self.map { gestures in
  57. let translationView = view.targetView(for: gestures.panGesture)
  58. let translation = gestures.panGesture.translation(in: translationView)
  59. let transform = CGAffineTransform.identity
  60. .rotated(by: gestures.rotationGesture.rotation)
  61. .scaledBy(x: gestures.pinchGesture.scale, y: gestures.pinchGesture.scale)
  62. .translatedBy(x: translation.x, y: translation.y)
  63. let velocity = TransformVelocity(
  64. translation: gestures.panGesture.velocity(in: translationView),
  65. rotation: gestures.rotationGesture.velocity,
  66. scale: gestures.pinchGesture.velocity
  67. )
  68. return (transform, velocity)
  69. }
  70. }
  71. }