Skip to content

Commit

Permalink
Swipe and dismiss feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
abdullahselek committed Feb 15, 2018
1 parent e91f20d commit d28311d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
44 changes: 44 additions & 0 deletions SwiftyNotifications/SwiftyNotifications.swift
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions SwiftyNotificationsTests/SwiftyNotificationsTests.swift
Expand Up @@ -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()
Expand Down

0 comments on commit d28311d

Please sign in to comment.