我一直在尝试实现一个按钮与两个不同的触摸事件。比方说,当用户点击按钮(触摸很短的时间),它会触发actionTapped,当用户长时间触摸按钮时会触发actionTouched。一个按钮与两个不同的触摸事件
This link可能会给出一个想法,但它会使动作重复一遍又一遍。
我一直在尝试实现一个按钮与两个不同的触摸事件。比方说,当用户点击按钮(触摸很短的时间),它会触发actionTapped,当用户长时间触摸按钮时会触发actionTouched。一个按钮与两个不同的触摸事件
This link可能会给出一个想法,但它会使动作重复一遍又一遍。
好的,这是我的解决方案。 scheduledTimerWithTimeInterval是最小间隔,使得它触及:
- (IBAction) micButtonTouchedDownAction {
self.micButtonTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(micButtonAction:) userInfo:nil repeats:YES];
self.micButtonReleased = FALSE;
}
- (IBAction) micButtonTouchedUpInsideAction {
self.micButtonReleased = TRUE;
}
- (IBAction) micButtonTouchedUpOutsideAction {
self.micButtonReleased = TRUE;
}
- (void) micButtonAction:(NSTimer *)timer {
[self.micButtonTimer invalidate];
self.micButtonTimer = nil;
if(self.micButtonReleased) {
NSLog(@"Tapped");
}
else {
NSLog(@"Touched");
}
}
我将做到以下几点:
touchDownTime
)。在你触摸了方法:计算time elapsed = current time - touchDownTime
2.1转换为秒
2.2如果时间流逝>需要时间做动作1(长按),做别的动作2(短触摸)
iOS SDK包含两个适合您需求的手势识别器:
没有任何动作附加到其创建您的按钮。然后创建两个手势识别器,每个类型中的一个,每个都映射到您想要的动作。然后将手势识别器附加到按钮上。
检查此[http://stackoverflow.com/questions/4013896/detecting-long-tap-on-iphone](http://stackoverflow.com/ questions/4013896/detected-long-tap-on-iphone) – 2012-04-19 12:35:06