2015-06-21 49 views
1

好的,这是一个棘手的问题。当用户点击并保持在屏幕上时,我需要提供丰富的警报框。奇怪的点击并保持在SpriteKit行为

下面是我用它来做到这一点(在Objective-C SpriteKit)代码:

在我touchesBegan:withEvent:方法,我有这样的:

tapBegin = [NSDate date]; 

猜测,使一个NSDate对象与当前日期和时间(Objective-C和Apple相当于DateTime date = Now)。

在我touchesEnded:withEvent:方法,我有这样的:

NSDate *endTap = [NSDate date]; 
NSDateComponents *comps = [[NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian] components:NSCalendarUnitSecond fromDate:tapBegin toDate:endTap options:0]; 
if (comps.second >= 1) { 
    // tap and hold event 
} else { 
    // normal tap event 
} 

结果应该是这样的:如果用户的水龙头,并拥有一些超过一秒钟,if的说法应该是true,如果没有,那么它应该是false

实际结果有一些奇怪的现象:如果用户试图点击某些东西,他们会随机获得点击并保持事件。我如何解决这个问题?

回答

1

如果你想获得两个日期之间的区别,那么你应该改为使用timeIntervalSinceDate: 我会改变touchesEnded:withEvent:以下几点:

NSDate *endTap = [NSDate date]; 
NSTimeInterval diff = [tapBegin timeIntervalSinceDate:endTap]; 
NSTimeInterval threshold = 1.0; 
if (diff >= threshold) { 
    // tap and hold event 
} else { 
    // normal tap event 
}