0

ボタン付きの方向パッドを作成するコードを調べて見つけましたが、機能させるにはボタンを常にタップする必要があります。だから、上に行きたいのなら、常に押し上げなければなりませんでした。ユーザーがボタンを押したままオブジェクトを移動するにはどうすればよいですか。これは私がすでに持っているボタンのコードです

-(IBAction)moveLeft:(id)sender{
Robot.center = CGPointMake(Robot.center.x-10, Robot.center.y);
[UIView animateWithDuration:0.5 animations:^{
    Robot.center = CGPointMake(Robot.center.x, Robot.center.y+10);
}];
}

-(IBAction)moveRight:(id)sender{
Robot.center = CGPointMake(Robot.center.x+10, Robot.center.y);
[UIView animateWithDuration:0.5 animations:^{
    Robot.center = CGPointMake(Robot.center.x, Robot.center.y+10);
}];
}

-(IBAction)moveUp:(id)sender{
Robot.center = CGPointMake(Robot.center.x, Robot.center.y-10);
[UIView animateWithDuration:0.5 animations:^{
    Robot.center = CGPointMake(Robot.center.x, Robot.center.y+10);
 }];
}

-(IBAction)moveDown:(id)sender{
Robot.center = CGPointMake(Robot.center.x, Robot.center.y+10);
[UIView animateWithDuration:0.5 animations:^{
    Robot.center = CGPointMake(Robot.center.x, Robot.center.y+10);
}];
}
4

2 に答える 2

1

UIControlEventTouchDownコントロール イベントを使用してメソッドの実行を開始しUIControlEventTouchUpInsideたり、ボタンが「押されていない」ことを検出したりできます。

ボタンへのアクションを設定します。例:

[myButton addTarget:self action:@selector(startButtonTouch:) forControlEvents:UIControlEventTouchDown];
[myButton addTarget:self action:@selector(endButtonTouch:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];

(上記により、ボタンの内側と外側のタッチアップによって endButtonTouch: メソッドが呼び出されることに注意してください。)

次に、次のように startButtonTouch: および endButtonTouch メソッドを追加します。

- (void)startButtonTouch:(id)sender {
    // start the process running...
}

- (void)endButtonTouch:(id)sender {
// stop the running process...
}

ユーザーがボタンの外にドラッグしたときに終了する場合は、同様に追加UIControlEventTouchDragExitします。

于 2014-10-21T04:49:31.300 に答える