From f162983b7006748ae2ffefac146e286e7e8e75c1 Mon Sep 17 00:00:00 2001 From: ghysrc Date: Fri, 13 May 2016 15:10:44 +0800 Subject: [PATCH] add single tap to dismiss --- SKPhotoBrowser/SKDetectingImageView.swift | 32 +++++++++++------------ SKPhotoBrowser/SKZoomingScrollView.swift | 11 +++----- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/SKPhotoBrowser/SKDetectingImageView.swift b/SKPhotoBrowser/SKDetectingImageView.swift index b62b01b..3119bea 100644 --- a/SKPhotoBrowser/SKDetectingImageView.swift +++ b/SKPhotoBrowser/SKDetectingImageView.swift @@ -9,8 +9,8 @@ import UIKit @objc protocol SKDetectingImageViewDelegate { - func handleImageViewSingleTap(view: UIImageView, touch: UITouch) - func handleImageViewDoubleTap(view: UIImageView, touch: UITouch) + func handleImageViewSingleTap(touchPoint: CGPoint) + func handleImageViewDoubleTap(touchPoint: CGPoint) } class SKDetectingImageView: UIImageView { @@ -23,24 +23,22 @@ class SKDetectingImageView: UIImageView { override init(frame: CGRect) { super.init(frame: frame) userInteractionEnabled = true + let doubleTap = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap(_:))) + doubleTap.numberOfTapsRequired = 2 + doubleTap.numberOfTouchesRequired = 1 + self.addGestureRecognizer(doubleTap) + let singleTap = UITapGestureRecognizer(target: self, action: #selector(handleSingleTap(_:))) + singleTap.numberOfTapsRequired = 1 + singleTap.numberOfTouchesRequired = 1 + singleTap.requireGestureRecognizerToFail(doubleTap) + self.addGestureRecognizer(singleTap) } - override func touchesEnded(touches: Set, withEvent event: UIEvent?) { - super.touchesEnded(touches, withEvent: event) - - let touch = touches.first! - switch touch.tapCount { - case 1 : handleSingleTap(touch) - case 2 : handleDoubleTap(touch) - default: break - } - nextResponder() + func handleDoubleTap(recognizer:UITapGestureRecognizer) { + delegate?.handleImageViewDoubleTap(recognizer.locationInView(self)) } - func handleSingleTap(touch: UITouch) { - delegate?.handleImageViewSingleTap(self, touch: touch) - } - func handleDoubleTap(touch: UITouch) { - delegate?.handleImageViewDoubleTap(self, touch: touch) + func handleSingleTap(recognizer:UITapGestureRecognizer) { + delegate?.handleImageViewSingleTap(recognizer.locationInView(self)) } } \ No newline at end of file diff --git a/SKPhotoBrowser/SKZoomingScrollView.swift b/SKPhotoBrowser/SKZoomingScrollView.swift index 7ca8ba0..8f45e82 100644 --- a/SKPhotoBrowser/SKZoomingScrollView.swift +++ b/SKPhotoBrowser/SKZoomingScrollView.swift @@ -306,14 +306,11 @@ public class SKZoomingScrollView: UIScrollView, UIScrollViewDelegate, SKDetectin } // MARK: - SKDetectingImageViewDelegate - func handleImageViewSingleTap(view: UIImageView, touch: UITouch) { - if photoBrowser?.areControlsHidden() == false && photoBrowser?.enableSingleTapDismiss == true { - photoBrowser?.determineAndClose() - } - photoBrowser?.toggleControls() + func handleImageViewSingleTap(touchPoint: CGPoint) { + photoBrowser?.performCloseAnimationWithScrollView(self) } - func handleImageViewDoubleTap(view: UIImageView, touch: UITouch) { - handleDoubleTap(touch.locationInView(view)) + func handleImageViewDoubleTap(touchPoint: CGPoint) { + handleDoubleTap(touchPoint) } } \ No newline at end of file