diff --git a/Example/RxGesture-OSX/ViewController.swift b/Example/RxGesture-OSX/ViewController.swift index 62b701e..2bf80bd 100644 --- a/Example/RxGesture-OSX/ViewController.swift +++ b/Example/RxGesture-OSX/ViewController.swift @@ -17,9 +17,9 @@ class Step { let title: String let code: String - let install: (NSView, NSTextField, AnyObserver, DisposeBag) -> Void + let install: (NSView, NSTextField, @escaping () -> Void, DisposeBag) -> Void - init(title: String, code: String, install: @escaping (NSView, NSTextField, AnyObserver, DisposeBag) -> Void) { + init(title: String, code: String, install: @escaping (NSView, NSTextField, @escaping () -> Void, DisposeBag) -> Void) { self.title = title self.code = code self.install = install @@ -60,9 +60,9 @@ class MacViewController: NSViewController { func newIndex(for index: Int, action: Step.Action) -> Int { switch action { case .previous: - return index > 0 ? index - 1 : steps.count - 1 + return (steps.count + index - 1) % steps.count case .next: - return index < steps.count - 1 ? index + 1 : 0 + return (steps.count + index + 1) % steps.count } } @@ -100,7 +100,7 @@ class MacViewController: NSViewController { code.string = step.code myViewText.stringValue = "" - step.install(myView, myViewText, nextStepObserver.asObserver(), stepBag) + step.install(myView, myViewText, { [nextStepObserver] in nextStepObserver.onNext(.next) }, stepBag) print("active gestures: \(myView.gestureRecognizers.count)") } @@ -125,7 +125,7 @@ class MacViewController: NSViewController { .leftClickGesture() .when(.recognized) .subscribe(onNext: { _ in - nextStep.onNext(.next) + nextStep() }) .disposed(by: stepBag) }) @@ -154,7 +154,7 @@ class MacViewController: NSViewController { } .when(.recognized) .subscribe(onNext: { _ in - nextStep.onNext(.next) + nextStep() }) .disposed(by: stepBag) }) @@ -179,7 +179,7 @@ class MacViewController: NSViewController { .rightClickGesture() .when(.recognized) .subscribe(onNext: { _ in - nextStep.onNext(.next) + nextStep() }) .disposed(by: stepBag) }) @@ -204,7 +204,7 @@ class MacViewController: NSViewController { .anyGesture(.leftClick(), .rightClick()) .when(.recognized) .subscribe(onNext: { _ in - nextStep.onNext(.next) + nextStep() }) .disposed(by: stepBag) }) @@ -229,7 +229,7 @@ class MacViewController: NSViewController { .pressGesture() .when(.began) .subscribe(onNext: { _ in - nextStep.onNext(.next) + nextStep() }) .disposed(by: stepBag) }) @@ -276,7 +276,7 @@ class MacViewController: NSViewController { (.leftClick(), when: .recognized) ) .subscribe(onNext: { _ in - nextStep.onNext(.next) + nextStep() }) .disposed(by: stepBag) }) @@ -312,7 +312,7 @@ class MacViewController: NSViewController { .when(.changed) .asRotation() .subscribe(onNext: { rotation in - label.stringValue = String(format: "angle: %.2f", rotation) + label.stringValue = String(format: "%.0fº", rotation * 180 / .pi) view.layer!.transform = CATransform3DMakeRotation(rotation, 0, 0, 1) }) .disposed(by: stepBag) @@ -323,7 +323,7 @@ class MacViewController: NSViewController { (.leftClick(), when: .recognized) ) .subscribe(onNext: { _ in - nextStep.onNext(.next) + nextStep() }) .disposed(by: stepBag) }) @@ -370,7 +370,7 @@ class MacViewController: NSViewController { (.leftClick(), when: .recognized) ) .subscribe(onNext: { _ in - nextStep.onNext(.next) + nextStep() }) .disposed(by: stepBag) }) @@ -380,7 +380,7 @@ class MacViewController: NSViewController { private extension NSView { func animateTransform(to transform: CATransform3D) { - let initialTransform = self.layer!.transform + let initialTransform = self.layer?.presentation()?.transform ?? self.layer!.model().transform let anim = CABasicAnimation(keyPath: "transform") anim.duration = 0.5 @@ -391,7 +391,7 @@ private extension NSView { } func animateBackgroundColor(to color: NSColor) { - let initialColor = self.layer!.backgroundColor! + let initialColor = self.layer?.presentation()?.backgroundColor ?? self.layer?.model().backgroundColor let anim = CABasicAnimation(keyPath: "backgroundColor") anim.duration = 0.5 diff --git a/Example/RxGesture.xcodeproj/project.pbxproj b/Example/RxGesture.xcodeproj/project.pbxproj index 1789ed3..7f40bba 100644 --- a/Example/RxGesture.xcodeproj/project.pbxproj +++ b/Example/RxGesture.xcodeproj/project.pbxproj @@ -209,6 +209,7 @@ TargetAttributes = { 607FACCF1AFB9204008FA782 = { CreatedOnToolsVersion = 6.3.1; + DevelopmentTeam = JKFCB4CN7C; LastSwiftMigration = 1020; ProvisioningStyle = Manual; }; @@ -527,14 +528,14 @@ ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = "$(inherited)"; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_STYLE = Manual; - DEVELOPMENT_TEAM = ""; + DEVELOPMENT_TEAM = JKFCB4CN7C; INFOPLIST_FILE = RxGesture/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 9.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; MODULE_NAME = ExampleApp; PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; - PROVISIONING_PROFILE_SPECIFIER = ""; + PROVISIONING_PROFILE_SPECIFIER = "Wildcard Development"; SWIFT_SWIFT3_OBJC_INFERENCE = Default; SWIFT_VERSION = 5.0; }; @@ -547,14 +548,14 @@ ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = "$(inherited)"; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_STYLE = Manual; - DEVELOPMENT_TEAM = ""; + DEVELOPMENT_TEAM = JKFCB4CN7C; INFOPLIST_FILE = RxGesture/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 9.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; MODULE_NAME = ExampleApp; PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; - PROVISIONING_PROFILE_SPECIFIER = ""; + PROVISIONING_PROFILE_SPECIFIER = "Wildcard Development"; SWIFT_SWIFT3_OBJC_INFERENCE = Default; SWIFT_VERSION = 5.0; }; diff --git a/Example/RxGesture/Images.xcassets/AppIcon.appiconset/Contents.json b/Example/RxGesture/Images.xcassets/AppIcon.appiconset/Contents.json index d3942e9..8121323 100644 --- a/Example/RxGesture/Images.xcassets/AppIcon.appiconset/Contents.json +++ b/Example/RxGesture/Images.xcassets/AppIcon.appiconset/Contents.json @@ -2,37 +2,52 @@ "images" : [ { "idiom" : "iphone", - "size" : "29x29", - "scale" : "2x" + "scale" : "2x", + "size" : "20x20" }, { "idiom" : "iphone", - "size" : "29x29", - "scale" : "3x" + "scale" : "3x", + "size" : "20x20" }, { "idiom" : "iphone", - "size" : "40x40", - "scale" : "2x" + "scale" : "2x", + "size" : "29x29" }, { "idiom" : "iphone", - "size" : "40x40", - "scale" : "3x" + "scale" : "3x", + "size" : "29x29" }, { "idiom" : "iphone", - "size" : "60x60", - "scale" : "2x" + "scale" : "2x", + "size" : "40x40" }, { "idiom" : "iphone", - "size" : "60x60", - "scale" : "3x" + "scale" : "3x", + "size" : "40x40" + }, + { + "idiom" : "iphone", + "scale" : "2x", + "size" : "60x60" + }, + { + "idiom" : "iphone", + "scale" : "3x", + "size" : "60x60" + }, + { + "idiom" : "ios-marketing", + "scale" : "1x", + "size" : "1024x1024" } ], "info" : { - "version" : 1, - "author" : "xcode" + "author" : "xcode", + "version" : 1 } } diff --git a/Example/RxGesture/ViewController.swift b/Example/RxGesture/ViewController.swift index cf9f3d7..a9d3b4c 100644 --- a/Example/RxGesture/ViewController.swift +++ b/Example/RxGesture/ViewController.swift @@ -13,14 +13,16 @@ import RxGesture class Step { enum Action { case previous, next } - + typealias InitialState = (alpha: CGFloat, color: UIColor, transform: CGAffineTransform) let title: String let code: String - let install: (UIView, UILabel, AnyObserver, DisposeBag) -> Void + let initialState: InitialState + let install: (UIView, UILabel, @escaping () -> Void, DisposeBag) -> Void - init(title: String, code: String, install: @escaping (UIView, UILabel, AnyObserver, DisposeBag) -> Void) { + init(title: String, code: String, initialState: InitialState, install: @escaping (UIView, UILabel, @escaping () -> Void, DisposeBag) -> Void) { self.title = title self.code = code + self.initialState = initialState self.install = install } } @@ -40,29 +42,26 @@ class ViewController: UIViewController { super.viewDidLoad() navigationController?.navigationBar.shadowImage = UIImage() - var steps: [Step] = [ + let steps: [Step] = [ tapStep, doubleTapStep, swipeDownStep, swipeHorizontallyStep, longPressStep, touchDownStep, + forceTouchStep, panStep, pinchStep, rotateStep, transformStep ] - if #available(iOS 9.0, *), let index = steps.firstIndex(where: { $0 === panStep }) { - steps.insert(forceTouchStep, at: index) - } - func newIndex(for index: Int, action: Step.Action) -> Int { switch action { case .previous: - return index > 0 ? index - 1 : steps.count - 1 + return (steps.count + index - 1) % steps.count case .next: - return index < steps.count - 1 ? index + 1 : 0 + return (steps.count + index + 1) % steps.count } } @@ -70,17 +69,17 @@ class ViewController: UIViewController { .scan(0, accumulator: newIndex) .startWith(0) .map { (steps[$0], $0) } - .subscribe(onNext: { [unowned self] in self.updateStep($0, at: $1) }) + .subscribe(onNext: { [unowned self] step, index in + self.updateStep(step, at: index) + }) .disposed(by: bag) } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) - guard #available(iOS 9.0, *), - let superview = myView.superview, - let window = view.window - else { return } - + guard let superview = myView.superview, let window = view.window else { + return + } window.addSubview(myView) myView.centerXAnchor.constraint(equalTo: superview.centerXAnchor).isActive = true myView.centerYAnchor.constraint(equalTo: superview.centerYAnchor).isActive = true @@ -99,9 +98,17 @@ class ViewController: UIViewController { info.text = "\(index + 1). " + step.title code.text = step.code - + myViewText.text = nil - step.install(myView, myViewText, nextStepObserver.asObserver(), stepBag) + myViewText.numberOfLines = 1 + + UIView.animate(withDuration: 0.5, delay: 0, options: .beginFromCurrentState) { + self.myView.alpha = step.initialState.alpha + self.myView.backgroundColor = step.initialState.color + self.myView.transform = step.initialState.transform + } + + step.install(myView, myViewText, { [nextStepObserver] in nextStepObserver.onNext(.next) }, stepBag) print("active gestures: \(myView.gestureRecognizers?.count ?? 0)") } @@ -117,16 +124,13 @@ class ViewController: UIViewController { }) .disposed(by: disposeBag) """, + initialState: (1.0, .red, .identity), install: { view, _, nextStep, stepBag in - - view.animateTransform(to: .identity) - view.animateBackgroundColor(to: .red) - view.rx .tapGesture() .when(.recognized) .subscribe(onNext: { _ in - nextStep.onNext(.next) + nextStep() }) .disposed(by: stepBag) }) @@ -144,18 +148,15 @@ class ViewController: UIViewController { }) .disposed(by: disposeBag) """, + initialState: (1.0, .green, .identity), install: { view, _, nextStep, stepBag in - - view.animateTransform(to: .identity) - view.animateBackgroundColor(to: .green) - view.rx .tapGesture { gesture, _ in gesture.numberOfTapsRequired = 2 } .when(.recognized) .subscribe(onNext: { _ in - nextStep.onNext(.next) + nextStep() }) .disposed(by: stepBag) }) @@ -171,16 +172,13 @@ class ViewController: UIViewController { }) .disposed(by: disposeBag) """, + initialState: (1.0, .blue, .identity), install: { view, _, nextStep, stepBag in - - view.animateTransform(to: .identity) - view.animateBackgroundColor(to: .blue) - view.rx .swipeGesture(.down) .when(.recognized) .subscribe(onNext: { _ in - nextStep.onNext(.next) + nextStep() }) .disposed(by: stepBag) }) @@ -196,16 +194,13 @@ class ViewController: UIViewController { }) .disposed(by: disposeBag) """, + initialState: (1.0, .blue, CGAffineTransform(scaleX: 1.0, y: 2.0)), install: { view, _, nextStep, stepBag in - - view.animateTransform(to: CGAffineTransform(scaleX: 1.0, y: 2.0)) - view.animateBackgroundColor(to: .blue) - view.rx .swipeGesture(.left, .right) .when(.recognized) .subscribe(onNext: { _ in - nextStep.onNext(.next) + nextStep() }) .disposed(by: stepBag) }) @@ -221,16 +216,13 @@ class ViewController: UIViewController { }) .disposed(by: disposeBag) """, + initialState: (1.0, .blue, CGAffineTransform(scaleX: 2.0, y: 2.0)), install: { view, _, nextStep, stepBag in - - view.animateTransform(to: CGAffineTransform(scaleX: 2.0, y: 2.0)) - view.animateBackgroundColor(to: .blue) - view.rx .longPressGesture() .when(.began) .subscribe(onNext: { _ in - nextStep.onNext(.next) + nextStep() }) .disposed(by: stepBag) }) @@ -246,16 +238,13 @@ class ViewController: UIViewController { }) .disposed(by: disposeBag) """, + initialState: (1.0, .green, .identity), install: { view, _, nextStep, stepBag in - - view.animateTransform(to: .identity) - view.animateBackgroundColor(to: .green) - view.rx .touchDownGesture() .when(.began) .subscribe(onNext: { _ in - nextStep.onNext(.next) + nextStep() }) .disposed(by: stepBag) }) @@ -282,11 +271,8 @@ class ViewController: UIViewController { }) .disposed(by: stepBag) """, + initialState: (0.25, .red, .identity), install: { view, label, nextStep, stepBag in - - view.animateTransform(to: .identity) - view.animateBackgroundColor(to: .red) - let forceTouch = view.rx .forceTouchGesture() .share(replay: 1) @@ -303,9 +289,8 @@ class ViewController: UIViewController { forceTouch .when(.ended) - .subscribe(onNext: { [unowned view] _ in - view.alpha = 1 - nextStep.onNext(.next) + .subscribe(onNext: { _ in + nextStep() }) .disposed(by: stepBag) @@ -334,11 +319,8 @@ class ViewController: UIViewController { }) .disposed(by: disposeBag) """, + initialState: (1.0, .blue, .identity), install: { view, label, nextStep, stepBag in - - view.animateTransform(to: .identity) - view.animateBackgroundColor(to: .blue) - let panGesture = view.rx .panGesture() .share(replay: 1) @@ -355,7 +337,7 @@ class ViewController: UIViewController { panGesture .when(.ended) .subscribe(onNext: { _ in - nextStep.onNext(.next) + nextStep() }) .disposed(by: stepBag) }) @@ -382,11 +364,8 @@ class ViewController: UIViewController { }) .disposed(by: disposeBag) """, + initialState: (1.0, .blue, .identity), install: { view, label, nextStep, stepBag in - - view.animateTransform(to: .identity) - view.animateBackgroundColor(to: .blue) - let rotationGesture = view.rx .rotationGesture() .share(replay: 1) @@ -395,7 +374,7 @@ class ViewController: UIViewController { .when(.possible, .began, .changed) .asRotation() .subscribe(onNext: { rotation, _ in - label.text = String(format: "%.2f rad", rotation) + label.text = String(format: "%.fº", rotation * 180 / .pi) view.transform = CGAffineTransform(rotationAngle: rotation) }) .disposed(by: stepBag) @@ -403,7 +382,7 @@ class ViewController: UIViewController { rotationGesture .when(.ended) .subscribe(onNext: { _ in - nextStep.onNext(.next) + nextStep() }) .disposed(by: stepBag) }) @@ -428,12 +407,11 @@ class ViewController: UIViewController { }) .disposed(by: disposeBag) """, + initialState: (1.0, .blue, .identity), install: { view, label, nextStep, stepBag in - - view.animateTransform(to: .identity) - view.animateBackgroundColor(to: .blue) - - let pinchGesture = view.rx.pinchGesture().share(replay: 1) + let pinchGesture = view.rx + .pinchGesture() + .share(replay: 1) pinchGesture .when(.possible, .began, .changed) @@ -447,7 +425,7 @@ class ViewController: UIViewController { pinchGesture .when(.ended) .subscribe(onNext: { _ in - nextStep.onNext(.next) + nextStep() }) .disposed(by: stepBag) }) @@ -472,12 +450,11 @@ class ViewController: UIViewController { }) .disposed(by: disposeBag) """, + initialState: (1.0, .blue, .identity), install: { view, label, nextStep, stepBag in - - view.animateTransform(to: .identity) - view.animateBackgroundColor(to: .blue) - - let transformGestures = view.rx.transformGestures().share(replay: 1) + let transformGestures = view.rx + .transformGestures() + .share(replay: 1) transformGestures .when(.possible, .began, .changed) @@ -492,13 +469,11 @@ class ViewController: UIViewController { transformGestures .when(.ended) .subscribe(onNext: { _ in - label.numberOfLines = 1 - nextStep.onNext(.next) + nextStep() }) .disposed(by: stepBag) }) - @available(iOS 9, *) private func makeImpact(on forceTouch: Observable, stepBag: DisposeBag) { // It looks like #available(iOS 10.0, *) is ignored in the lazy var declaration ¯\_(ツ)_/¯ @@ -513,18 +488,3 @@ class ViewController: UIViewController { .disposed(by: stepBag) } } - -private extension UIView { - - func animateTransform(to transform: CGAffineTransform) { - UIView.animate(withDuration: 0.5) { - self.transform = transform - } - } - - func animateBackgroundColor(to color: UIColor) { - UIView.animate(withDuration: 0.5) { - self.backgroundColor = color - } - } -} diff --git a/RxGesture.podspec b/RxGesture.podspec index d7e79c6..1b4234d 100644 --- a/RxGesture.podspec +++ b/RxGesture.podspec @@ -17,7 +17,7 @@ Pod::Spec.new do |s| s.homepage = "https://github.com/RxSwiftCommunity/RxGesture" s.license = 'MIT' - s.authors = { "Marin Todorov" => "touch-code-magazine@underplot.com", "Jérôme Alves" => "j.alves@me.com" } + s.authors = { "RxSwiftCommunity" => "https://github.com/RxSwiftCommunity", "Jérôme Alves" => "j.alves@me.com" } s.source = { :git => "https://github.com/RxSwiftCommunity/RxGesture.git", :tag => s.version.to_s } s.requires_arc = true