Skip to content

Instantly share code, notes, and snippets.

@garohussenjian
Created July 12, 2016 04:27
Show Gist options
  • Save garohussenjian/78f42f1bab143e571c21d00c41439ded to your computer and use it in GitHub Desktop.
Save garohussenjian/78f42f1bab143e571c21d00c41439ded to your computer and use it in GitHub Desktop.
Stateful Gesture Recognizer subclass
//
// PanSelectGestureRecognizer.swift
//
// Created by Garo Hussenjian on 6/24/15.
//
//
import UIKit
import UIKit.UIGestureRecognizerSubclass
class PanSelectGestureRecognizer: UIGestureRecognizer {
var selectionState: Bool?
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent:event);
if state == .Possible {
state = .Began;
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent) {
super.touchesMoved(touches, withEvent: event)
state = .Changed
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent) {
super.touchesEnded(touches, withEvent: event)
state = .Ended
}
override func touchesCancelled(touches: Set<UITouch>, withEvent event: UIEvent) {
super.touchesCancelled(touches, withEvent: event)
state = .Cancelled
}
}
@garohussenjian
Copy link
Author

garohussenjian commented Jul 12, 2016

Here's how this is integrated at the callback site:

func panSelectGestureRecognized(sender: PanSelectGestureRecognizer) {
        if let pulseView = hitTest(sender.locationInView(self), withEvent: nil) as? PulseView {
            switch sender.state {
            case .Possible, .Began:
                sender.selectionState = !pulseView.selected
                sender.selectionState! ? selectPulseView(pulseView) : deselectPulseView(pulseView)

            case .Changed:
                if pulseView.selected != sender.selectionState {
                    sender.selectionState! ? selectPulseView(pulseView) : deselectPulseView(pulseView)
                }

            case .Ended, .Cancelled, .Failed:
                delegate?.matrixViewDidEndBatchSelection(self)
                sender.selectionState = nil
            }
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment