iOS:如何获得长按手势的持续时间?

我正在进行一个游戏,其中通过长时间按压对象本身来设置游戏对象的属性。 属性的值由长按手势的持续时间决定。 我使用UILongPressGestureRecognizer来达到这个目的,所以它是这样的:

[gameObjectView addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handle:)]]; 

然后处理函数

 - (void)handle:(UILongPressGestureRecognizer)gesture { if (gesture.state == UIGestureRecognizerStateEnded) { // Get the duration of the gesture and calculate the value for the attribute } } 

在这种情况下,如何获得长按手势的持续时间?

我很确定这个手势不会存储这些信息供您访问。 您只能在其上设置一个名为minimumPressDuration的属性,即识别手势之前的时间量。

解决方法与ios 5(未经testing):

创build一个名为timer的NSTimer属性: @property (nonatomic, strong) NSTimer *timer;

和一个计数器: @property (nonatomic, strong) int counter;

然后@synthesize

 - (void)incrementCounter { self.counter++; } - (void)handle:(UILongPressGestureRecognizer)gesture { if (gesture.state == UIGestureRecognizerStateBegan) { self.counter = 0; self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(incrementCounter) userInfo:nil repeats:yes]; } if (gesture.state == UIGestureRecognizerStateEnded) { [self.timer invalidate]; } } 

所以当手势开始时,每一秒都会启动一个计时器,直到手势结束。 在这种情况下,您需要将minimumPressDuration设置为0,否则手势不会马上开始。 然后做任何你想要的柜台!

没有计时器需要。 你可以这样做:

 - (void)handleRecognizer:(UILongPressGestureRecognizer *)gesture { static NSTimeInterval pressStartTime = 0.0; //This an be moved out and be kept as a property switch ([gesture state]) { case UIGestureRecognizerStateBegan: //Keeping start time... pressStartTime = [NSDate timeIntervalSinceReferenceDate]; break; /* edit*/ case UIGestureRecognizerStateEnded: { //Calculating duration NSTimeInterval duration = [NSDate timeIntervalSinceReferenceDate] - pressStartTime; //Note that NSTimeInterval is a double value... NSLog(@"Duration : %f",duration); break; } default: break; } } 

如果您想获得长按的实际持续时间,也不要忘记在创build手势识别器的minimumPressDuration将其设置为0
myLongPressGestureRecognizer.minimumPressDuration = 0

看来目前为止,面向对象的Cocoa Touch中最清洁和最简单的解决scheme是UILongPressGesture的子类。 这里是一个用Swift编写的例子。

  class MyLongPressGesture : UILongPressGestureRecognizer { var startTime : NSDate? } func installGestureHandler() { let longPress = MyLongPressGesture(target: self, action: "longPress:") button.addGestureRecognizer(longPress) } @IBAction func longPress(gesture: MyLongPressGesture) { if gesture.state == .Began { gesture.startTime = NSDate() } else if gesture.state == .Ended { let duration = NSDate().timeIntervalSinceDate(gesture.startTime!) println("duration was \(duration) seconds") } } 

如果您想包含第一次点击的时间,则可以在计算持续时间时添加该时间,方法是添加后面的gesture.minimumPressDuration。 缺点是,它可能不是微秒精确的,因为手势被触发和你的.Start处理器被调用之间可能有一小段(微小的)时间stream逝。 但对于绝大多数应用程序来说,这并不重要。

请参阅“minimumPressDuration”属性。 根据文件:

手指必须按下视图才能识别手势。

[…]

时间间隔以秒为单位。 默认的持续时间是0.5秒。

我知道这是一个迟到的答案,但这完全适合我在IOS 7和8,而无需创build一个计时器。

 UILongPressGestureRecognizer *longGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(yourAction)]; // create the recognizer longGR.minimumPressDuration = 10.0f; //Ten Seconds longGR.allowableMovement = 50.0f; //Allowable Movement while being pressed [gameObjectView setUserInteractionEnabled:YES]; //If setting interaction to a non-interactable object such as a UIImageView [gameObjectView addGestureRecognizer:longGR]; //Add the gesture recognizer to your object 

你可以通过在Swift 3.0中得到它

逻辑:只需指定按下的时间,并找出触摸结束时的时间差异

代码:

 //variable to calculate the press time static var pressStartTime: TimeInterval = 0.0 func handleRecognizer(gesture: UILongPressGestureRecognizer) -> Double { var duration: TimeInterval = 0 switch (gesture.state) { case .began: //Keeping start time... Browser.pressStartTime = NSDate.timeIntervalSinceReferenceDate case .ended: //Calculating duration duration = NSDate.timeIntervalSinceReferenceDate - Browser.pressStartTime //Note that NSTimeInterval is a double value... print("Duration : \(duration)") default: break; } return duration }