如何在UIScrollview中检测双击?
我试过了:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if (touch.tapCount == 2) {
// some operations here
}
}
但是在UIScrollView中没有检测到双击,有没有其他方法来检测双击?
此致
答案 0 :(得分:17)
如果您的目标是3.2+,则可以尝试使用UITapGestureRecognizer
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:yourViewController action:@selector(tapGesture:)];
tap.numberOfTapsRequired = 2;
tap.numberOfTouchesRequired = 1;
[scrollView addGestureRecognizer:tap];
[tap release];
然后你在viewcontroller中处理点击:
- (void)tapGesture:(UIGestureRecognizer*)gesture {
// Do something
}
答案 1 :(得分:6)
使用以下代码进行双击。
-(void) viewDidLoad{
// other necessary operations
UITapGestureRecognizer* dTap = [[UITapGestureRecognizer alloc] initWithTarget : self action : @selector (doubleTap:)];
[doubleTap setDelaysTouchesBegan : YES];
dTap.numberOfTapsRequired = 2;
dTap.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer : dTap];
[dTap release];
}
- (void) doubleTap : (UIGestureRecognizer*) sender
{
NSLog (@"Double tap Do operations here...");
}
答案 2 :(得分:2)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSUInteger numTaps = [[touches anyObject] tapCount];
if (numTaps== 2) {
//some operations here
}
}
使用此代码