JavaScript,iPhone:按住按钮重复动作

时间:2010-04-24 05:47:12

标签: javascript iphone

我正在开发一个我想在iPhone上工作的网站,但是我想要它,这样他们就可以点按并按住按钮并让它继续触发onclick事件。我让它在其他浏览器中工作,但iPhone是唯一需要按住按钮的人。按住按钮有没有办法重复功能?感谢。

1 个答案:

答案 0 :(得分:0)

根据从按钮发送的touchDown和touchUpInside消息启动和停止计时器。假设按钮被设置为将其touchDown和touchUpInside方法链接到下面的方法。你也可以设置一个标志“BOOL firing = TRUE;”触摸时触摸下来并取消设置。你的游戏循环可以查看那个标志并做出决定。

@interface TouchTest : UIViewController {
NSTimer *touchDownTimer;
}

@property (nonatomic,retain)  NSTimer   *touchDownTimer;

- (IBAction) touchDown: (id) sender;
- (IBAction) touchUp: (id) sender;

@end

@implementation TouchTest
@synthesize touchDownTimer;

- (void)viewDidLoad {
 [super viewDidLoad];

 UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 button.frame = CGRectMake(100.00, 100.00, 105.0, 37.0);
 [button setTitle:@"Fire at Will" forState:UIControlStateNormal];
 [button addTarget:(id)self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];
 [button addTarget:(id)self action:@selector(touchUp:) forControlEvents:UIControlEventTouchUpInside];

 [self.view addSubview:button];
 }

- (IBAction) fireWeapon {
    NSLog(@"WeaponFired");
}

- (IBAction) touchDown :(id) sender{
    touchDownTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval).3 target:self selector:@selector(fireWeapon) userInfo:nil repeats:TRUE];
}

- (IBAction) touchUp :(id) sender {
    [touchDownTimer invalidate];
}
@end