Wie kann ich eine Ansicht geladen werden basierend darauf, wie lange halte ich eine UIButton?
-
22-09-2019 - |
Frage
Ich arbeite auf einem iOS-App, wo ich will einen Blick laden, wenn ein UIButton für x Sekunden gehalten wird, eine andere, wenn es für x + y Sekunden lang gehalten, usw. fand ich ein Tutorial. Das Problem, das ich in heißt renne, wie schalte ich die Länge der Taste drücken? Das Tutorial geschaltet, um die Anzahl der Abgriffe.
-(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;
}
}
Irgendwelche Vorschläge?
Lösung
habe ich meine Antwort. Ich habe gerade eine NSTimer auf einem Touch-Down-Ereignis, und blieb auf Ausbesserungs innen gestartet.
// 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];
}
Andere Tipps
Verwenden Sie die UILongPressGestureRecognizer Klasse. Apple hat dies in 3.2 dokumentiert - http://developer.apple.com/iphone/prerelease/library/documentation/General/Conceptual/iPadProgrammingGuide/GestureSupport/GestureSupport.html#//apple_ref/doc/uid/TP40009370-CH5-SW1