1

私は、UIButtonがx秒間押し続けられた場合に、あるビューをロードしたい、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;
    }
}

助言がありますか?

4

2 に答える 2

1

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

// 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];
}
于 2010-04-08T16:10:17.720 に答える
0

UILongPressGestureRecognizerクラスを使用します。Appleはこれを3.2で文書化しています -http://developer.apple.com/iphone/prerelease/library/documentation/General/Conceptual/iPadProgrammingGuide/GestureSupport/GestureSupport.html#//apple_ref/doc/uid/TP40009370-CH5-SW1

于 2010-03-25T19:54:45.770 に答える