我使用 Xcode 7.2 目前为 watchos2 编码,对于我的应用程序,我只希望按钮在 0.5 秒内双击时执行一个方法,如果用户只单击一次,则它不会执行该操作。例如:
在我的 .h
#import <WatchKit/WatchKit.h>
#import <Foundation/Foundation.h>
@interface InterfaceController : WKInterfaceController
- (IBAction)btnActivate;
@property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceButton *activateBtn;
@end
在我的 .m 文件的顶部,我声明:
int taps = 1;
- (IBAction)btnActivate {
taps = taps + 1;
if (taps == 2){
[activateBtn setBackgroundColor:[UIColor grayColor]];
[self performSelector:@selector(tapOne:) withObject:self afterDelay:0.5];
}
else if (taps == 3){
[self performSelector:@selector(tapTwo:) withObject:self afterDelay:0];
[activateBtn setEnabled:NO];
[activateBtn setBackgroundColor:[UIColor redColor]];
} else {
//Do other things here if you want
}
}
- (void) tapOne: (id) sender{
if (taps == 2){
[self performSelector:@selector(afterTapDone:) withObject:self afterDelay:1];
} else {
}
}
- (void) tapTwo: (id) sender {
//Perform button actions here when user double taps
[self performSelector:@selector(afterTapDone:) withObject:self afterDelay:10];
}
- (void) afterTapDone: (id) sender {
taps = 1;
[activateBtn setEnabled:YES];
[activateBtn setBackgroundColor:[UIColor colorWithRed:0/255.0f green:174/255.0f blue:239/255.0f alpha:1.0f]];
[activateBtn setTitle:@"Activate"];
}
当然,您可以调整 afterDelays 等,并清理代码,但这就是我能够让双击工作的方式