更新时间:2023-09-20 21:01:40
任何人都知道如何获得连续,固定(非移动)下触的识别。不使用按钮,使用多点触摸和NSTimer:
Don't use a button, use multi-touch and NSTimer:
在接口中创建一个视图本地NSTimer对象,然后使用它来启动/取消计时器
Make a view-local NSTimer object inside your interface, then use it to start/cancel the timer
-(void)movePlayer:(id)sender {
<Code to move player>
}
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
timer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(movePlayer:) userInfo:nil repeats:YES];
}
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
if (timer != nil)
[timer invalidate];
timer = nil;
}
-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
if (timer != nil) {
[timer invalidate];
timer = nil;
}
}
这样,间隔,而不必依赖于按钮,并获得重复的行为,你正在寻找。
注意touchesMoved触发器 - 如果他们移动他们的手指,这取消了计时器,播放器停止移动。
This way, you can repeat the event at a predefined interval, and not have to rely on a button, and get the repeat behaviour you're looking for. Note the touchesMoved trigger - if they move their finger, this cancels the timer, and the player stops moving.