如何延长点击以处理连续触摸?
Posted
技术标签:
【中文标题】如何延长点击以处理连续触摸?【英文标题】:How to extend tap to handle continuous touches? 【发布时间】:2014-01-16 09:40:00 【问题描述】:当屏幕被“点击”时,我正在使用下面的代码发射弹丸(在基于 Sprite Kit 的游戏中),这一切正常。但是我想扩展它,以便在用户“触摸并按住屏幕”时重复调用 handleTap 有人能指出我正确的方向吗?
// INITIAL SETUP
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setNumberOfTouchesRequired:1];
[view addGestureRecognizer:tapRecognizer];
.
// WHEN TAPPED
- (void)handleTap:(UITapGestureRecognizer *)recognizer
NSLog(@"%s", __PRETTY_FUNCTION__);
[self setupAndFireProjectile];
【问题讨论】:
【参考方案1】:改用UILongPressGestureRecognizer
:
// INITIAL SETUP
UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouch:)];
[view addGestureRecognizer:recognizer];
.
// WHEN TAPPED
- (void)handleTouch:(UILongPressGestureRecognizer *)recognizer
NSLog(@"%s", __PRETTY_FUNCTION__);
[self setupAndFireProjectile];
或者使用UIResponder
'stouchesBegan:withEvent:
和touchesEnded:withEvent:
方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
// Start shooting
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
// End shooting
【讨论】:
谢谢安德烈,非常感谢。以上是关于如何延长点击以处理连续触摸?的主要内容,如果未能解决你的问题,请参考以下文章
如何在小部件上连续点击一次并在小部件像钢琴一样触摸钢琴键时执行一些操作?