在压入和持有情况下使UIButton不断燃烧的方法? [英] Way to make a UIButton continuously fire during a press-and-hold situation?
问题描述
您知道当您按住D-Pad上的右键时,Mario是如何继续向右移动的?以同样的方式,我希望我的UIButton持续触发它的动作,持续它的持续时间。这是可能的UIButton吗?如果不是,这是否可能与UIImageView通过以某种方式重写触摸处理方法?实际上,在尝试做到这一点与UIButton做之前,我有一些UIImageViews(排列作为一个D-Pad),通过触摸处理方法检查,但事情开始变得凌乱所以我认为这可以通过使用UIButton更容易,切换。
You know how Mario just keeps running to the right when you press and hold the right-button on the D-Pad? In the same manner, I want my UIButton to continuously fire its action for the duration that it is held down. Is this possible for a UIButton? If not, is this possible to do with a UIImageView by overriding a touch handling method in a certain way? Actually, before trying to do get this done with UIButton I had some UIImageViews (Arranged to function as a D-Pad) that were checked by touch handling methods but things started to get messy so I thought this could be done easier with UIButton and thus switched over. Anybody who knows how to get recognition of a continuous, stationary (not-moved) down-touch, please share.
推荐答案
任何人都知道如何获得连续,固定(非移动)下触的识别。不使用按钮,使用多点触摸和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.
这篇关于在压入和持有情况下使UIButton不断燃烧的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!