From d28311d3cb0c59fd5afcc5a306b67dd4e9c3ed18 Mon Sep 17 00:00:00 2001 From: Abdullah Selek Date: Thu, 15 Feb 2018 22:26:38 +0000 Subject: [PATCH] Swipe and dismiss feature. --- SwiftyNotifications/SwiftyNotifications.swift | 44 +++++++++++++++++++ .../SwiftyNotificationsTests.swift | 5 +++ 2 files changed, 49 insertions(+) diff --git a/SwiftyNotifications/SwiftyNotifications.swift b/SwiftyNotifications/SwiftyNotifications.swift index 21f05ed..bd09be3 100644 --- a/SwiftyNotifications/SwiftyNotifications.swift +++ b/SwiftyNotifications/SwiftyNotifications.swift @@ -248,6 +248,12 @@ open class SwiftyNotifications: UIView { let tapHandler = UITapGestureRecognizer(target: self, action: #selector(SwiftyNotifications.handleTap)) addGestureRecognizer(tapHandler) } + + open func addSwipeGestureRecognizer(direction: UISwipeGestureRecognizerDirection) { + let swipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(dismissView(gesture:))) + swipeGestureRecognizer.direction = direction + self.addGestureRecognizer(swipeGestureRecognizer) + } open override func didMoveToSuperview() { super.didMoveToSuperview() @@ -376,6 +382,44 @@ open class SwiftyNotifications: UIView { @objc internal func handleTap() { touchHandler?() } + + @objc func dismissView(gesture: UISwipeGestureRecognizer) { + guard let window = UIApplication.shared.keyWindow, let view = gesture.view else { + return + } + var frame: CGRect! + switch gesture.direction { + case .right: + frame = CGRect(x: view.frame.width, + y: view.frame.origin.y, + width: view.frame.width, + height: view.frame.height) + break + case .left: + frame = CGRect(x: -view.frame.width, + y: view.frame.origin.y, + width: view.frame.width, + height: view.frame.height) + break + case .up: + frame = CGRect(x: 0.0, + y:-view.frame.height, + width: view.frame.width, + height: view.frame.height) + break + case .down: + frame = CGRect(x: 0.0, + y: window.frame.height + view.frame.height, + width: view.frame.width, + height: view.frame.height) + break + default: + break + } + UIView.animate(withDuration: 0.4) { + view.frame = frame + } + } internal func canDisplay() -> Bool { return self.superview != nil ? true : false diff --git a/SwiftyNotificationsTests/SwiftyNotificationsTests.swift b/SwiftyNotificationsTests/SwiftyNotificationsTests.swift index 1911769..b6b5962 100644 --- a/SwiftyNotificationsTests/SwiftyNotificationsTests.swift +++ b/SwiftyNotificationsTests/SwiftyNotificationsTests.swift @@ -240,6 +240,11 @@ class SwiftyNotificationsTests: XCTestCase { let gesturesCount = swiftNotifications.gestureRecognizers?.count XCTAssertEqual(gesturesCount, 1) } + + func testAddSwipeGestureRecognizer() { + swiftNotifications.addSwipeGestureRecognizer(direction: .left) + XCTAssertEqual(swiftNotifications.gestureRecognizers?.count, 1) + } func testAddWidthConstraint() { let viewController = UIViewController()