ボタンと画像があります。ボタンを押し続けると、画像が画面全体にすばやく表示されます。今、押し続けると手放すだけで動きます。一定時間後に動き出し、手放すと止まる必要があります。これが私が持っているものです:
- (IBAction)buttonDown:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
x = x + 100;
}
ボタンと画像があります。ボタンを押し続けると、画像が画面全体にすばやく表示されます。今、押し続けると手放すだけで動きます。一定時間後に動き出し、手放すと止まる必要があります。これが私が持っているものです:
- (IBAction)buttonDown:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
x = x + 100;
}
以下のコードでは、_imageViewを移動するために3秒押す必要があります。その後、画像が移動を開始し、ボタンを離すと画像が停止します。
- (IBAction)pushToMove:(id)sender {
[self performSelector:@selector(move) withObject:nil afterDelay:3.0];
}
- (void)move
{
_nt = [NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(goRight)
userInfo:nil
repeats:YES];
}
- (void)goRight
{
_imageView.frame = CGRectMake(_imageView.frame.origin.x + 10, _imageView.frame.origin.y,
_imageView.frame.size.width, _imageView.frame.size.height);
}
- (IBAction)stop
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(move) object:nil];
[_nt invalidate];
}
このサンプルプロジェクトをGitHubに配置しました。ダウンロードして実行するだけです。
これは、内部をタッチアップするのではなく、ボタンのタッチダウンプロパティを好むだけで、InterfaceBuilderで実行できます。コードでは、次のようになります。
[myButton addTarget:self action:@selector(myMethod) forControlEvents:UIControlEventTouchDown];
これに加えて、アニメーションの状態チェックを行いUIViewAnimationOptionBeginFromCurrentState
、ユーザーがボタンをもう一度押したときに中断したところから再開できるようにするオプションとして使用することをお勧めします。