Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

「こする」を表現するカスタムUIGestureRecognizer

More than 5 years have passed since last update.

必要になったので作ってみました。
必ずしも左右にこすらなくても反応してしまうのはご愛嬌で。
あと最初ハマったのでこちらも参照ください。
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
    }
}

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away