HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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(未经测试)的解决方法

创建一个名为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,否则手势将不会立即开始.然后做任何你想要的柜台!

大佬总结

以上是大佬教程为你收集整理的iOS:如何获得长按手势的持续时间?全部内容,希望文章能够帮你解决iOS:如何获得长按手势的持续时间?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。