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
}
}
在这种情况下如何获取长按手势的持续时间?
I'm working on a game in which an attribute of a game object is set by long pressing on the object itself. The value of the attribute is determined by the duration of the long press gesture. I'm using UILongPressGestureRecognizer for this purpose, so it's something like this:
[gameObjectView addGestureRecognizer:[[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handle:)]];
Then the handler function
- (void)handle:(UILongPressGestureRecognizer)gesture {
if (gesture.state == UIGestureRecognizerStateEnded) {
// Get the duration of the gesture and calculate the value for the attribute
}
}
How do I get the duration of the long press gesture in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我很确定该手势不会存储此信息供您访问。您只能在其上设置一个名为“minimumPressDuration”的属性,该属性是识别手势之前的时间量。
ios 5(未经测试)的解决方法:
创建一个名为timer的NSTimer属性:
@property(nonatomic,strong)NSTimer *timer;
和一个计数器:
@property(nonatomic,strong)int counter;
然后
@synthesize
因此,当手势开始时,启动一个计时器,每秒触发增量方法,直到手势结束。在这种情况下,您需要将
minimumPressDuration
设置为 0,否则手势不会立即开始。然后用计数器做任何你想做的事!I'm pretty sure the gesture doesn't store this information for you to access. You can only set a property on it called minimumPressDuration that is the amount of time before the gesture is recognised.
Workaround with ios 5 (untested):
Create an NSTimer property called timer:
@property (nonatomic, strong) NSTimer *timer;
And a counter:
@property (nonatomic, strong) int counter;
Then
@synthesize
So when the gesture begins start a timer that fires the incrementation method every second until the gesture ends. In this case you'll want to set the
minimumPressDuration
to 0 otherwise the gesture won't start straight away. Then do whatever you want with counter!无需计时器。您可以通过以下方式实现:
另外,如果您想获得长按的实际持续时间,请不要忘记在创建手势识别器时将其
minimumPressDuration
设置为0
按:myLongPressGestureRecognizer.minimumPressDuration = 0
No timers needed. You can achieve it this way:
Also, don't forget to set the gesture recognizer's
minimumPressDuration
to0
while creating it, if you want to get the real duration of the long press:myLongPressGestureRecognizer.minimumPressDuration = 0
到目前为止,面向对象的 Cocoa Touch 中最干净、最简单的解决方案似乎是子类化 UILongPressGesture。这是一个用 Swift 编写的示例。
如果您想包含从第一次点击开始的时间,您可以在计算持续时间时通过添加 backgesture.minimumPressDuration 来包含它。缺点是,它可能不是微秒级的精确度,因为在触发手势和调用 .Start 处理程序之间可能需要很短的时间。但对于绝大多数应用程序来说这并不重要。
It seems by far the cleanest and simplest solution in object oriented Cocoa Touch is to subclass UILongPressGesture. Here is an example written in Swift.
If you want to include the time from first tap, you can include it when you calculate duration, by adding back gesture.minimumPressDuration. The drawback is that it is probably not be micro-second precise, given there is likely a small (tiny) amount of time elapsing between the gesture being triggered and your .Start handler being called. But for the vast majority of applications that shouldn't matter.
请参阅“minimumPressDuration”属性。根据文档:
See the "minimumPressDuration" property. According to the documentation:
我知道这是一个迟到的答案,但这对我来说在 IOS 7 和 iOS 7 中非常适合。 8 无需创建计时器。
I know this is a late answer but this works perfectly for me in IOS 7 & 8 without having to create a timer.
您可以通过Swift 3.0中的以下内容来获取它
逻辑:只需分配按下时间并找到触摸结束时的时间差
代码:
You can get it by Following in Swift 3.0
Logic : Just assigning the time of press and find the difference of time when touch ends
Code :
似乎在新的 iOS 版本中(目前是 10),
.begin
和.ending
状态的长按识别器回调会相继发生,仅在事件结束后,即仅当您从屏幕上抬起手指时。这使得两个事件之间的差异只有几分之一毫秒,而不是您实际搜索的差异。
在这个问题得到解决之前,我决定用 swift 从头开始创建另一个手势识别器,这将是它的工作框架。
然后您可以轻松访问
LongPressDurationGestureRecognizer
手势识别器的.duration
属性。例如:
这个具体示例没有考虑实际发生的触摸次数或其位置。但您可以轻松地使用它,扩展 LongPressDurationGestureRecognizer。
It seems that with new iOS versions (10 for me at the moment), the long press recognizer callbacks for both the
.begin
and.ended
states happen one after the other, only after the event has ended, that is only when you raise your finger from the screen.That makes the difference between the two events be fractions of milliseconds, and not what you were actually searching for.
Until this will be fixed, I decided to create another gesture recognizer from scratch in swift, and that would be its working skeleton.
Then you can access the
.duration
property of theLongPressDurationGestureRecognizer
gesture recognizer easily.For instance:
This specific example, doesn't take into consideration how many touches actually took place, or their location. But you can easily play with it, extending the LongPressDurationGestureRecognizer.