0

私は3秒間押している間にボタンの色を変えようとしています。タイマーが3秒に達すると、色の変更は永久的になりますが、時間が経過する前にユーザーがボタンを離すと、ボタンは元の色に戻ります。私はこれまで持っていることはこれです:viewDidLoadfireSomeMethodボタンを押したままUIButtonの色を変更し、x秒前にリリースされた場合は色の変更をキャンセルする方法はありますか?

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] 
              initWithTarget:self 
              action:@selector(fireSomeMethod)]; 
longPress.minimumPressDuration = 3.0; 
[self.view addGestureRecognizer:longPress]; 

で 私は

- (void)someMethod { 
     [UIView transitionWithView:self.button 
        duration:2.0 
        options:UIViewAnimationOptionCurveEaseIn 
       animations:^{ 
        self.button.backgroundColor = [UIColor redColor]; 
       } 
       completion:^(BOOL finished) { 
        NSLog(@"animation finished"); 
       }]; 
} 

これは発射するアニメーションのため3秒間押しボタンを保持するために私を必要とし、アニメーション自体は2を取る持っています完了する秒数。望ましい動作は、longPressが開始されるとアニメーションが開始され、3秒前にボタンを放すとすべてが元の状態に戻ります。おかげで事前

答えて

2

であなたの助けあなたのボタンの2アクションを行いますUILongPressGestureRecognizer

を使用する必要のボタンイベントの使用をしないために、一つはTouch Down用で、もう一方は、この

// For `Touch Up Inside` 
- (IBAction)btnReleased:(id)sender { 
    [timer invalidate]; 
    NSLog(@"time - %d",timeStarted); 
} 
// For `Touch Down` 
- (IBAction)btnTouchedDown:(id)sender { 
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0f 
            target:self 
            selector:@selector(_timerFired:) 
            userInfo:nil 
            repeats:YES]; 
    timeStarted = 0; 
} 

- (void)_timerFired:(NSTimer *)timer {\ 
    timeStarted++; 
} 
よう Touch Up Inside

のためであります

NSTimerタイプのtimerおよびintタイプのtimeStartedを作成します。 Touch Downのタイマーを起動してTouch Up Insideに無効にし、Touch Up Insideのアクションメソッドでは、ボタンが押されるまでの合計時間を取得します。上記のコードに示されているように、

関連する問題