我正在研究一种游戏,其中通过长按对象本身来设置游戏对象的属性。属性的值由长按手势的持续时间确定。我正在使用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
}
}
在这种情况下如何获得长按手势的持续时间?
答案 0 :(得分:26)
我很确定手势不会存储此信息供您访问。您只能在其上设置名为minimumPressDuration的属性,该属性是识别手势之前的时间。
使用ios 5(未经测试)的解决方法:
创建一个名为timer:@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,否则手势将不会立即开始。然后用计数器做任何你想做的事情!
答案 1 :(得分:8)
不需要计时器。你可以用这种方式实现它:
- (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
答案 2 :(得分:6)
到目前为止,面向对象的Cocoa Touch中最干净,最简单的解决方案似乎是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")
}
}
如果您想要包括第一次点击的时间,可以在计算持续时间时通过添加回手势来包括它.minimumPressDuration。鉴于在被触发的手势和被调用的.Start处理程序之间可能存在少量(微小的)时间,缺点是它可能不是微秒精确的。但对于绝大多数应用而言并不重要。
答案 3 :(得分:4)
请参阅“minimumPressDuration”属性。根据文件:
手指必须按下手势视图的最小时间段 被承认。
[...]
时间间隔以秒为单位。默认持续时间为0.5 秒。
答案 4 :(得分:1)
我知道这是一个迟到的答案,但这对我来说在IOS 7& 8,无需创建计时器。
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
答案 5 :(得分:1)
您可以通过 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
}
答案 6 :(得分:0)
似乎在新的iOS版本(目前对我来说是10)上,ul{
padding-left: 0;
}
和.begin
状态的长按识别器回调一个接一个地发生,只有在事件发生后结束,只有当您从屏幕上抬起手指时。
这会使两个事件之间的差值以毫秒为单位,而不是您实际搜索的时间。
在此问题解决之前,我决定迅速从头开始创建另一个手势识别器,这就是它的工作原理。
.ended
然后,您可以轻松访问import UIKit.UIGestureRecognizerSubclass
class LongPressDurationGestureRecognizer : UIGestureRecognizer {
private var startTime : Date?
private var _duration = 0.0
public var duration : Double {
get {
return _duration
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
startTime = Date() // now
state = .begin
//state = .possible, if you would like the recongnizer not to fire any callback until it has ended
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
_duration = Date().timeIntervalSince(self.startTime!)
print("duration was \(duration) seconds")
state = .ended
//state = .recognized, if you would like the recongnizer not to fire any callback until it has ended
}
}
手势识别器的.duration
属性。
例如:
LongPressDurationGestureRecognizer
此特定示例未考虑实际发生了多少触摸或其位置。但是您可以轻松地使用它,扩展LongPressDurationGestureRecognizer。