必要になったので作ってみました。
必ずしも左右にこすらなくても反応してしまうのはご愛嬌で。
あと最初ハマったのでこちらも参照ください。
http://qiita.com/okitsutakatomo/items/37e717f2b34a957cca8f
//
// UINaderuGestureRecognizer.swift
//
// Created by Takatomo Okitsu on 2014/12/25.
// Copyright (c) 2014年 Takatomo Okitsu. All rights reserved.
//
import UIKit
var kNaderuGestureReturnCount = 2
var kNaderuGestureReturnDistance:CGFloat = 20
class UINaderuGestureRecognizer :UIGestureRecognizer {
var timeOutTimer:NSTimer?
var startingPoint:CGPoint?
var returnCount:Int = 0
override func reset() {
startingPoint = nil
returnCount = 0
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.invalidateTimer()
if touches.count > 1 {
self.state = UIGestureRecognizerState.Failed
} else {
let touch:UITouch = touches.anyObject()? as UITouch
if touch.tapCount > 1 {
self.state = .Failed
} else {
return
}
}
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
let touch:UITouch = touches.anyObject()? as UITouch
if self.state == .Possible {
if startingPoint == nil {
startingPoint = touch.locationInView(nil)
timeOutTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "handleTimeOut", userInfo: nil, repeats: false)
} else {
let point = touch.locationInView(nil)
let distance = (point - startingPoint!).length
if distance > kNaderuGestureReturnDistance {
returnCount++
if returnCount >= kNaderuGestureReturnCount {
self.state = UIGestureRecognizerState.Ended //event fired
} else {
startingPoint = point
}
}
}
} else {
self.state = .Failed
}
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
self.invalidateTimer()
self.state = .Failed
}
override func touchesCancelled(touches: NSSet, withEvent event: UIEvent) {
self.invalidateTimer()
self.state = .Cancelled
}
func invalidateTimer() {
timeOutTimer?.invalidate()
timeOutTimer = nil;
}
func handleTimeOut() {
self.invalidateTimer()
self.state = .Failed
}
}