首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >iOS:如何获取长按手势的持续时间?

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

Stack Overflow用户
提问于 2012-02-24 02:32:38
回答 5查看 30.9K关注 0票数 18

我正在开发一个游戏,其中游戏对象的属性是通过长按对象本身来设置的。该属性的值由长按手势的持续时间确定。我使用UILongPressGestureRecognizer来实现这个目的,所以它是这样的:

代码语言:javascript
复制
[gameObjectView addGestureRecognizer:[[UILongPressGestureRecognizer alloc] 
                                       initWithTarget:self action:@selector(handle:)]];

然后处理程序函数

代码语言:javascript
复制
- (void)handle:(UILongPressGestureRecognizer)gesture {
  if (gesture.state == UIGestureRecognizerStateEnded) {
    // Get the duration of the gesture and calculate the value for the attribute
  }
}

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

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2012-02-24 02:37:39

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

使用ios 5(未经测试)的解决方法:

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

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

然后是@synthesize

代码语言:javascript
复制
- (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,否则手势不会立即开始。然后用counter做任何你想做的事情!

票数 29
EN

Stack Overflow用户

发布于 2016-05-29 21:01:30

不需要计时器。您可以通过以下方式实现:

代码语言:javascript
复制
- (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;
    }
}

此外,如果你想获得长按的真实持续时间,不要忘记在创建手势识别器时将它的minimumPressDuration设置为0

myLongPressGestureRecognizer.minimumPressDuration = 0

票数 9
EN

Stack Overflow用户

发布于 2015-02-18 05:42:48

到目前为止,在面向对象的Cocoa Touch中,最干净、最简单的解决方案似乎是将UILongPressGesture子类化。下面是一个用Swift编写的示例。

代码语言:javascript
复制
    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")
            }
    }

如果您想包括从第一次点击开始的时间,您可以在计算持续时间时通过添加back gesture.minimumPressDuration将其包括在内。缺点是它可能不是微秒级的精确,因为在被触发的手势和被调用的.Start处理程序之间可能有一段很小(很小)的时间。但对于绝大多数应用程序来说,这并不重要。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9419041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档