ios - 如何扩展tap来处理连续触摸?

标签 ios sprite-kit uitapgesturerecognizer

当屏幕被“点击”时,我使用下面的代码来发射射弹(在基于 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];
}

最佳答案

使用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's touchesBegan:withEvent:touchesEnded:withEvent: 方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
     // Start shooting
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
     // End shooting
}

关于ios - 如何扩展tap来处理连续触摸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21158154/

相关文章:

ios - UILabel 看到最右边的内容

ios - applicationWillEnterForeground 的问题

ios - 将 UITapGestureRecognizer(双击)添加到 UIButton 会减慢将按钮图像设置为选中的速度

ios - Swift 2 TapGesture 错误 : "unrecognized selector sent to instance"

ios - Xcode 7 Swift 2 我应该使用按钮字体还是图像?

ios - 来自 AVCaptureSession 的 AVCaptureVideoPreviewLayer 随机停止视频流

ios - Facebook SDK v4 和解析 (Swift)

ios - SKShapeNode 使用错误的宽度和高度

sprite-kit - 尝试从WITHIN代码中更改我的SKEmitterNode发出的粒子的颜色

ios - 将值传递给 IBAction