Ios UIS开关上的UILongPressGestureRecognitor未启动

Ios UIS开关上的UILongPressGestureRecognitor未启动,ios,objective-c,cocoa-touch,uigesturerecognizer,uiswitch,Ios,Objective C,Cocoa Touch,Uigesturerecognizer,Uiswitch,今天,我尝试在ui开关中添加一个额外的ui长按手势识别器,但实际效果并不理想。我的代码如下: UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(doSomething:)]; [lpgr setDelaysTouchesBegan:YES]; [lpgr setDelaysTouchesEnded:YES]; [lpgr s

今天,我尝试在
ui开关
中添加一个额外的
ui长按手势识别器
,但实际效果并不理想。我的代码如下:

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(doSomething:)];
[lpgr setDelaysTouchesBegan:YES];
[lpgr setDelaysTouchesEnded:YES];
[lpgr setCancelsTouchesInView:YES];
[self.switcher addGestureRecognizer:lpgr];
viewController
viewDidLoad
方法中,该方法是该开关的父级
viewController
。(切换器是一个实例变量,通过故事板设置。
IBOutlet
从UISwitch正确设置到其viewController的切换器属性)

在其他几个控件上(如
UIButton、UISlider、UIStepper
等),即使没有取消或延迟触摸,也能正常工作,并完美地触发目标方法。然而,我的开关,它拒绝这种行为

我已尝试通过迭代该交换机上的所有手势识别器并调用
[switcher removeGestureRecognizer:…]
删除UI交换机上的任何其他手势识别器,我还将所有这些设置为要求我的LongPress手势识别器失败。其他类型的手势识别器也不会启动

据我所知,手势识别器将在它们所属的视图或控件接收触摸之前接收触摸。因此,
setCancelsTouchesInView:YES
setDelaysTouchesInView:YES
应该使手势识别器能够在视图不知道的情况下处理每个手势,直到失败或成功,对吗

--------------编辑------------------

我的方法的全部内容:

- (void)viewDidLoad
{
[super viewDidLoad];

UIGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(edit:)];
[self.view addGestureRecognizer:lpgr];

lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(edit:)];
[self.button addGestureRecognizer:lpgr];

lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(edit:)];
[self.slider addGestureRecognizer:lpgr];

lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(edit:)];
[self.segmentedControl addGestureRecognizer:lpgr];

lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(edit:)];
[self.switcher addGestureRecognizer:lpgr];

lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(edit:)];
[self.stepper addGestureRecognizer:lpgr];
}
正如我所说,对于其他控件,这正在工作,只有开关不想工作

尝试使用它

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

// Disallow recognition of tap gestures in the segmented control.
if ((touch.view == yourSwitch)) {//change it to your condition
    return NO;
}
return YES;
}

确保确认
UIGestureRecognitzerDelegate
以使其工作。

为什么不让GestureRecognitzer接收我的触摸?这与我想要的正好相反。连接到交换机的手势识别器没有接收到它应该接收的触摸。我没有为我的手势识别器设置委托,如果该方法未实现,默认值为YES。此外,我没有子类UISwitch,因此如果我想成为其他手势识别器的委托,我必须设置它们的委托。这应该与我尝试过的完全删除它们的效果相同;您禁用了交互,这可能(我不确定)也会影响手势识别器。对不起,实际上这只是我尝试不使用该行后的一个测试。它以前也不起作用