如何根据持有 UIButton 的时间加载视图?
-
22-09-2019 - |
题
我正在开发一个 iOS 应用程序,如果 UIButton 按住 x 秒,我想加载一个视图,如果按住 x+y 秒,则加载另一个视图,等等。我找到了一个教程。我遇到的问题是,如何切换按钮按下的长度?本教程切换了点击次数。
-(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,并在触摸了内停止。
// 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];
}
其他提示
使用 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
不隶属于 StackOverflow