Ios 如何检查按钮在一秒钟内按下多少次
我正在开发一个简单的苹果手表演示。我想检查一下按钮在一秒钟内按了多少次。Ios 如何检查按钮在一秒钟内按下多少次,ios,xcode,uibutton,counter,apple-watch,Ios,Xcode,Uibutton,Counter,Apple Watch,我正在开发一个简单的苹果手表演示。我想检查一下按钮在一秒钟内按了多少次。 就我的问题给我一些提示。 以下是我实现的一些代码: - (void)awakeWithContext:(id)context { [super awakeWithContext:context]; [btnBell setBackgroundImage:[UIImage imageNamed:@"Bell.png"]]; [self performSelector:@selector(checkCo
就我的问题给我一些提示。
以下是我实现的一些代码:
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
[btnBell setBackgroundImage:[UIImage imageNamed:@"Bell.png"]];
[self performSelector:@selector(checkCount) withObject:nil afterDelay:1.0];
NSLog(@"%@ awakeWithContext", self);
self.counter = 0;
}
- (void)willActivate {
// This method is called when watch view controller is about to be visible to user
NSLog(@"%@ will activate", self);
}
- (void)didDeactivate {
// This method is called when watch view controller is no longer visible
NSLog(@"%@ did deactivate", self);
}
#pragma mark - Button actions
- (IBAction)saveCounter {
//Send count to parent application
self.counter++;
NSString *counterString = [NSString stringWithFormat:@"%d", self.counter];
NSDictionary *applicationData = [[NSDictionary alloc] initWithObjects:@[counterString] forKeys:@[@"counterValue"]];
//Handle reciever in app delegate of parent app
[WKInterfaceController openParentApplication:applicationData reply:^(NSDictionary *replyInfo, NSError *error) {
[self showSaveNotificationLabel];
}];
}
#pragma mark - Helper methods
-(void)showSaveNotificationLabel {
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: @"beep1" ofType: @"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
myAudioPlayer.volume = 0.1;
[myAudioPlayer play];
}
-(void)checkCount
{
NSLog(@"Button tap clicked : %d times", self.counter);
self.counter = 0;
}
请随时询问我的代码。同时也给我一些建议。如果你在你的类中像这样做一个计数器和时间开始变量
[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(calculateClickRate)
userInfo:nil
repeats:YES];
-(void) calculateClickRate
{
float rate = clicks;
clicks = 0;
}
var pressesInOneSecond = 1
var timeStart: NSDate?
@IBAction func buttonWasPressed(sender: AnyObject) {
if timeStart == nil {
timeStart = NSDate()
} else {
let end = NSDate()
let timeInterval: Double = end.timeIntervalSinceDate(timeStart!)
if timeInterval < 1 {
pressesInOneSecond++
} else {
// One second has elapsed and the button press count
// is now stored in pressesInOneSecond.
print("presses in one second was \(pressesInOneSecond)")
}
}
}
你可以用这样的方法来计算你的按键动作
var pressesInOneSecond = 1
var timeStart: NSDate?
@IBAction func buttonWasPressed(sender: AnyObject) {
if timeStart == nil {
timeStart = NSDate()
} else {
let end = NSDate()
let timeInterval: Double = end.timeIntervalSinceDate(timeStart!)
if timeInterval < 1 {
pressesInOneSecond++
} else {
// One second has elapsed and the button press count
// is now stored in pressesInOneSecond.
print("presses in one second was \(pressesInOneSecond)")
}
}
}
为了解释其工作原理,NSDate()
提供当前时间,timeInterval
中记录的NSTimeInterval
是开始时间与结束时间(以秒为单位)的度量值。在本例中,计数限制设置为1s。计数从第一次按开始,1s后按不保存。全局变量:
int buttonTapCounts;
viewDidLoad方法:
[self performSelector:@selector(checkCount) withObject:nil afterDelay:1.0];
UIButton事件的方法:
-(iAction)点击按钮点击:(id)发件人
{
按钮计数++;
}
1秒后检查计数器:
-(void)checkCount
{
NSLog(@"Button tap clicked : %d times", buttonTapCounts);
// Resetting the button count
buttonTapCounts = 0;
}
感谢各位发布答案。
我使用代码解决了此问题。
这里是我的按钮点击方法:
- (IBAction)saveCounter {
[myAudioPlayer play];
//Send count to parent application
CurrentTime = [[NSDate date] timeIntervalSince1970] * 1000;
NSLog(@"Current Time: %f",CurrentTime);
if ((CurrentTime - StartTime) < 500)
{
myAudioPlayer.volume = myAudioPlayer.volume +0.1;
}
else
{
myAudioPlayer.volume = 0.5;
}
StartTime = CurrentTime;
//self.counter++;
}
-(iAction)保存计数器{
[myAudioPlayer播放];
//将计数发送到父应用程序
CurrentTime=[[NSDate date]时间间隔1970]*1000;
NSLog(@“当前时间:%f”,当前时间);
如果((当前时间-开始时间)<500)
{
myAudioPlayer.volume=myAudioPlayer.volume+0.1;
}
其他的
{
myAudioPlayer.volume=0.5;
}
开始时间=当前时间;
//self.counter++;
}
StartTime
和CurrentTime
是double
变量。在iAction方法中使用一个计数器变量并将其递增如何?是的,我知道,但如何检查时间间隔?你能解释一下为什么你会问这个问题,这个问题在你之前被问了两次吗?在前面的问题中仅仅评论是困难的吗?因为我也尝试了这些答案,但不幸的是它不起作用。所以,这就是为什么我发布了这个问题。@Roman Safin,顺便说一句,如果你没有任何问题的答案。不要否决投票。你是stackoverflow的创始人吗?我把performSelector放在savecounter方法中。但不工作。是否检查了方法是否正确执行?我将检查,我认为performselector不工作。performselector可能正在工作,但使用上下文唤醒的方法可能需要日志时间才能执行。尽量增加时间