UIButton を押し続ける時間に基づいてビューを読み込むにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/2518726

質問

私は、UIButton が x 秒間保持された場合に 1 つのビューをロードし、x+y 秒間保持された場合に別のビューをロードするなどの iOS アプリに取り組んでいます。チュートリアルを見つけました。私が直面している問題は、ボタンを押す長さをどうやって切り替えるかということです。チュートリアルではタップ数を切り替えました。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSSet *allTouches = [event allTouches];

    switch ([allTouches count]) 
    {
        case 1: // Single touch
        { 
            // Get the first touch.
            UITouch *touch = [[allTouches allObjects] objectAtIndex:0];

            switch ([touch tapCount])
            {
                case 1: // Single Tap.
                {
                    // Start a timer
                    timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(showAlertView:) userInfo:nil repeats:NO];
                    [timer retain];
                } 
                    break;
                case 2: // Double tap.
                    break;
            }
        } 
            break;
        case 2: // Double touch
        {
        } 
            break;
        default:
            break;
    }
}

助言がありますか?

役に立ちましたか?

解決

私は私の答えを得ました。私はちょうどイベントタッチダウンにNSTimerを開始し、内部にアップtouch上で停止しました。

// TRP - On Touch Down event, start the timer
-(IBAction) startTimer
{
    self.time = 0;
    // TRP - Start a timer
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];

    [timer retain];     // TRP - Retain timer so it is not accidentally deallocated

}

// TRP - Method to update the timer display
-(void)updateTimer
{
    time++;
    NSLog(@"Seconds: %i ", time); 
    if (15 == self.time)
        [timer invalidate];
}

// TRP - On Touch Up Inside event, stop the timer & display results
-(IBAction) btn_MediaMeterResults
{
    [timer invalidate];
    NSLog(@"time: %i ", self.time);

    ResultsViewController *resultsView = [[ResultsViewController alloc] initWithNibName:@"ResultsViewController" bundle:nil];

    // TRP - The following line is passing the "time" variable from MediaMasterViewController to ResultsViewController
    resultsView.time = self.time;

    [self.view addSubview:resultsView.view];
}

他のヒント

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top