2015-04-04 55 views
3

我有排一个UITableView,我添加单击手势与双击手势:如何通过双击来加速识别单个抽头?

let doubleTap = UITapGestureRecognizer(target: self, action: "doubleTap:") 
doubleTap.numberOfTapsRequired = 2 
doubleTap.numberOfTouchesRequired = 1 
tableView.addGestureRecognizer(doubleTap) 

let singleTap = UITapGestureRecognizer(target: self, action: "singleTap:") 
singleTap.numberOfTapsRequired = 1 
singleTap.numberOfTouchesRequired = 1 
singleTap.requireGestureRecognizerToFail(doubleTap) 
tableView.addGestureRecognizer(singleTap) 

有没有减少在第一轻叩由当手势识别之间的时间的方式实现,这是一次点击而不是双击。

我在问这个,因为当我点击一下时,新的viewController显得很晚,给人的感觉是应用程序滞后。

+0

不,您需要直接处理水龙头以更改计时 – Wain 2015-04-04 21:21:56

+0

我该怎么做?这是“最佳做法”吗? – Nico 2015-04-04 21:24:21

+0

在单元格上并不重要,可能您会使用实现触摸事件方法的自定义子视图。或者,改变你的UI不要使用双击 – Wain 2015-04-04 21:26:14

回答

5

我找到了答案,这个link

雨燕版本:

class UIShortTapGestureRecognizer: UITapGestureRecognizer { 
    let tapMaxDelay: Double = 0.3 

    override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) { 
     super.touchesBegan(touches, withEvent: event) 
     delay(tapMaxDelay) { 
      // Enough time has passed and the gesture was not recognized -> It has failed. 
      if self.state != UIGestureRecognizerState.Ended { 
       self.state = UIGestureRecognizerState.Failed 
      } 
     } 
    } 
} 

随着delay(delay: Double, closure:()->())

class func delay(delay:Double, closure:()->()) { 
     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), closure) 
    } 
+0

不要忘记在定义'UITapGestureRecognizer'子类的文件中添加'import UIKit.UIGestureRecognizerSubclass'。 (来自http://stackoverflow.com/a/26557518/341954) 否则你会得到构建错误:_“方法不覆盖任何方法从它的超类”_。 – PDK 2016-11-21 16:24:27

1

为今后全面实施霍华德阳,这里的链接: https://github.com/oney/SingleDoubleTapGestureRecognizer

let tap = SingleDoubleTapGestureRecognizer(target: self, singleAction: Selector("singleTap"), doubleAction: Selector("doubleTap")) 
     tap.duration = 0.8 
     view.addGestureRecognizer(tap) 

https://github.com/oney/SingleDoubleTapGestureRecognizer/blob/master/Pod/Classes/SingleDoubleTapGestureRecognizer.swift

// 
// SingleDoubleTapGestureRecognizer.swift 
// SingleDoubleTapGestureRecognizer 
// 
// Created by Howard Yang on 08/22/2015. 
// Copyright (c) 2015 Howard Yang. All rights reserved. 
// 

import UIKit 

public class SingleDoubleTapGestureRecognizer: UITapGestureRecognizer { 
    var targetDelegate: SingleDoubleTapGestureRecognizerDelegate 
    public var duration: CFTimeInterval = 0.3 { 
     didSet { 
      self.targetDelegate.duration = duration 
     } 
    } 
    public init(target: AnyObject, singleAction: Selector, doubleAction: Selector) { 
     targetDelegate = SingleDoubleTapGestureRecognizerDelegate(target: target, singleAction: singleAction, doubleAction: doubleAction) 
     super.init(target: targetDelegate, action: Selector("fakeAction:")) 
     numberOfTapsRequired = 1 
    } 
} 
class SingleDoubleTapGestureRecognizerDelegate: NSObject { 
    var target: AnyObject 
    var singleAction: Selector 
    var doubleAction: Selector 
    var duration: CFTimeInterval = 0.3 
    var tapCount = 0 

    init(target: AnyObject, singleAction: Selector, doubleAction: Selector) { 
     self.target = target 
     self.singleAction = singleAction 
     self.doubleAction = doubleAction 
    } 

    func fakeAction(g: UITapGestureRecognizer) { 
     tapCount = tapCount + 1 
     if tapCount == 1 { 
      delayHelper(duration, task: { 
       if self.tapCount == 1 { 
        NSThread.detachNewThreadSelector(self.singleAction, toTarget:self.target, withObject: g) 
       } 
       else if self.tapCount == 2 { 
        NSThread.detachNewThreadSelector(self.doubleAction, toTarget:self.target, withObject: g) 
       } 
       self.tapCount = 0 
      }) 
     } 
    } 
    typealias DelayTask = (cancel : Bool) ->() 

    func delayHelper(time:NSTimeInterval, task:()->()) -> DelayTask? { 

     func dispatch_later(block:()->()) { 
      dispatch_after(
       dispatch_time(
        DISPATCH_TIME_NOW, 
        Int64(time * Double(NSEC_PER_SEC))), 
       dispatch_get_main_queue(), 
       block) 
     } 

     var closure: dispatch_block_t? = task 
     var result: DelayTask? 

     let delayedClosure: DelayTask = { 
      cancel in 
      if let internalClosure = closure { 
       if (cancel == false) { 
        dispatch_async(dispatch_get_main_queue(), internalClosure); 
       } 
      } 
      closure = nil 
      result = nil 
     } 

     result = delayedClosure 

     dispatch_later { 
      if let delayedClosure = result { 
       delayedClosure(cancel: false) 
      } 
     } 

     return result; 
    } 

    func cancel(task:DelayTask?) { 
     task?(cancel: true) 
    } 
} 
+0

尽管这个链接可能回答这个问题,但最好在这里包含答案的基本部分,并提供供参考的链接。如果链接页面更改,则仅链接答案可能会失效。 – 2016-04-08 17:38:41

0

Markus's斯威夫特3版eladleb's原液充分落实。


创建子文件UIShortTapGestureRecogninzer

import UIKit 
import UIKit.UIGestureRecognizerSubclass 

class UIShortTapGestureRecognizer: UITapGestureRecognizer { 
    let tapMaxDelay: Double = 0.3 //anything below 0.3 may cause doubleTap to be inaccessible by many users 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) { 
     super.touchesBegan(touches, with: event) 

     DispatchQueue.main.asyncAfter(deadline: .now() + tapMaxDelay) { [weak self] in 
      if self?.state != UIGestureRecognizerState.recognized { 
       self?.state = UIGestureRecognizerState.failed 
      } 
     } 
    } 
} 

注:当添加UIGestureRecognizer只doubleTap必须型,需要UIShortTapGestureRecognizer & singleTap.require(toFail: doubleTap)的。

func addBoth (views: UIView, selectorSingle: Selector, selectorDouble: Selector) { 
    let doubleTap:UIShortTapGestureRecognizer = UIShortTapGestureRecognizer(target: self, action: selectorDouble) 
    doubleTap.numberOfTapsRequired = 2 
    views.addGestureRecognizer(doubleTap) 

    let singleTap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: selectorSingle) 
    singleTap.numberOfTapsRequired = 1 
    singleTap.require(toFail: doubleTap) 
    views.addGestureRecognizer(singleTap) 
}