gpt4 book ai didi

ios - SpriteKit 行为中的奇怪点击并按住

转载 作者:行者123 更新时间:2023-11-29 12:19:49 24 4
gpt4 key购买 nike

好的,这是一个棘手的问题。我希望在用户点击并按住屏幕时显示提示框。

这是我用来执行此操作的代码(在 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

实际结果有奇怪的行为:如果用户试图点击某物,他们会随机获得点击并按住事件。我该如何解决?

最佳答案

如果你想得到两个日期之间的差异,那么你应该使用 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
}

关于ios - SpriteKit 行为中的奇怪点击并按住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30960893/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com