Skip to content

Commit

Permalink
Make it harder to accidentally dismiss image preview.
Browse files Browse the repository at this point in the history
The swipe downwards now only triggers when lifting the finger, after having swiped far enough, and then only if the finger is lifted soon after it was initially pressed.
  • Loading branch information
nolanw committed Oct 21, 2014
1 parent 74f759d commit 043f3e8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
40 changes: 36 additions & 4 deletions Source/Images/ImageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import UIKit

/// Downloads an image and shows it in a zoomable scroll view for inspection and further action.
class ImageViewController: AwfulViewController {

let URL: NSURL!
var doneAction: (() -> Void)?
private var downloadTask: NSURLSessionTask!
Expand All @@ -17,10 +18,10 @@ class ImageViewController: AwfulViewController {
@IBOutlet private weak var activityIndicator: UIActivityIndicatorView!
@IBOutlet private weak var scrollView: UIScrollView!
@IBOutlet private weak var imageView: FLAnimatedImageView!
@IBOutlet private var swipeGestureRecognizer: UISwipeGestureRecognizer!
@IBOutlet private var overlaidViews: [UIView]!
@IBOutlet private weak var actionButton: UIButton!
@IBOutlet private weak var statusBarBackgroundViewHeightConstraint: NSLayoutConstraint!
@IBOutlet var panToDismissGestureRecognizer: UIPanGestureRecognizer!

init(URL: NSURL) {
self.URL = URL
Expand All @@ -33,7 +34,6 @@ class ImageViewController: AwfulViewController {

override func viewDidLoad() {
super.viewDidLoad()
scrollView.panGestureRecognizer.requireGestureRecognizerToFail(swipeGestureRecognizer)
fetchImage()
}

Expand Down Expand Up @@ -158,8 +158,30 @@ class ImageViewController: AwfulViewController {
dismiss()
}

@IBAction func didSwipeDown(sender: UISwipeGestureRecognizer) {
dismiss()
private var panStart: NSTimeInterval!

@IBAction func didPanToDismiss(sender: UIPanGestureRecognizer) {
switch sender.state {
case .Began:
panStart = NSProcessInfo.processInfo().systemUptime
case .Changed:
let velocity = sender.velocityInView(view)
if velocity.y < 0 || abs(velocity.x) > abs(velocity.y) {
sender.enabled = false
sender.enabled = true
}
case .Ended:
let translation = sender.translationInView(view)
if abs(translation.x) < 30 {
if translation.y > 60 {
if NSProcessInfo.processInfo().systemUptime - panStart < 0.5 {
dismiss()
}
}
}
default:
break;
}
}

private func setShowingOverlaidViews(showing: Bool, animated: Bool) {
Expand Down Expand Up @@ -209,6 +231,16 @@ class ImageViewController: AwfulViewController {
}
}

extension ImageViewController: UIGestureRecognizerDelegate {

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer == panToDismissGestureRecognizer {
return true
}
fatalError("unexpected gesture recognizer")
}
}

class ContentCenteringScrollView: UIScrollView {
override func layoutSubviews() {
super.layoutSubviews()
Expand Down
10 changes: 5 additions & 5 deletions Source/Images/ImageViewController.xib
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
<outlet property="actionButton" destination="1rR-c0-50m" id="6Yj-zG-QCB"/>
<outlet property="activityIndicator" destination="IJO-hy-jmY" id="ngd-Vo-Qfu"/>
<outlet property="imageView" destination="l6V-lC-TXd" id="KE4-5k-v2Q"/>
<outlet property="panToDismissGestureRecognizer" destination="6MC-Po-de8" id="juE-9E-ros"/>
<outlet property="scrollView" destination="ZLh-kG-l6m" id="tZs-7X-gYN"/>
<outlet property="statusBarBackgroundViewHeightConstraint" destination="oaK-Qa-JlQ" id="0o7-7T-UjP"/>
<outlet property="swipeGestureRecognizer" destination="w93-BF-zAY" id="uIJ-FG-UpT"/>
<outlet property="view" destination="i5M-Pr-FkT" id="sfx-zR-JGt"/>
<outletCollection property="overlaidViews" destination="5rZ-lk-hWQ" collectionClass="NSMutableArray" id="iIB-1I-Gc3"/>
<outletCollection property="overlaidViews" destination="1rR-c0-50m" collectionClass="NSMutableArray" id="DCy-UM-8xx"/>
Expand Down Expand Up @@ -106,20 +106,20 @@
<constraint firstItem="ZLh-kG-l6m" firstAttribute="bottom" secondItem="i5M-Pr-FkT" secondAttribute="bottom" id="rXK-ap-Kk7"/>
</constraints>
<connections>
<outletCollection property="gestureRecognizers" destination="w93-BF-zAY" appends="YES" id="Siy-5S-xww"/>
<outletCollection property="gestureRecognizers" destination="eQi-oZ-TBD" appends="YES" id="viY-NT-RgM"/>
<outletCollection property="gestureRecognizers" destination="6MC-Po-de8" appends="YES" id="IwH-8y-qJd"/>
</connections>
</view>
<tapGestureRecognizer id="eQi-oZ-TBD">
<connections>
<action selector="didTapImage:" destination="-1" id="YdM-RZ-VTa"/>
</connections>
</tapGestureRecognizer>
<swipeGestureRecognizer direction="down" id="w93-BF-zAY">
<panGestureRecognizer minimumNumberOfTouches="1" id="6MC-Po-de8" userLabel="Swipe to Dismiss Gesture Recognizer">
<connections>
<action selector="didSwipeDown:" destination="-1" id="ksH-Ez-MuY"/>
<action selector="didPanToDismiss:" destination="-1" id="dZy-7V-LEZ"/>
</connections>
</swipeGestureRecognizer>
</panGestureRecognizer>
</objects>
<resources>
<image name="action" width="19" height="26"/>
Expand Down

0 comments on commit 043f3e8

Please sign in to comment.