Skip to content

Commit

Permalink
Add pan gesture
Browse files Browse the repository at this point in the history
  • Loading branch information
amirpirzad committed Aug 28, 2020
1 parent 3810e74 commit ddfce2e
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Sources/Controllers/MessagesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
super.viewDidLoad()
setupDefaults()
setupSubviews()
addPanGesture()
setupConstraints()
setupDelegates()
addMenuControllerObservers()
Expand Down Expand Up @@ -148,6 +149,39 @@ UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

// MARK: - Methods [Private]

/// Display time of message by swiping the cell
private func addPanGesture() {
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(_:)))
messagesCollectionView.addGestureRecognizer(panGesture)
messagesCollectionView.clipsToBounds = false
}

@objc
private func handlePanGesture(_ gesture: UIPanGestureRecognizer) {
guard let parentView = gesture.view else {
return
}

switch gesture.state {
case .began, .changed:
// show time
let translation = gesture.translation(in: view)
let minX = -(view.frame.size.width * 0.3)
let maxX: CGFloat = 0
var offsetValue = translation.x
offsetValue = max(offsetValue, minX)
offsetValue = min(offsetValue, maxX)
parentView.frame.origin.x = offsetValue
case .ended:
// hide time
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.8, options: .curveEaseOut, animations: {
parentView.frame.origin.x = 0
}, completion: nil)
default:
break
}
}

private func setupDefaults() {
extendedLayoutIncludesOpaqueBars = true
view.backgroundColor = .collectionViewBackground
Expand Down

0 comments on commit ddfce2e

Please sign in to comment.