2014-03-30 84 views
1

我有两个VC的,无论是举办uicollectionviews,在其中的一个(称之为VC1)我通过应用程序生命周期中,用户可以从这个VC1到另一个VC2切换添加僵尸对象,requireGestureRecognizerToFail,为什么?

UILongPressGestureRecognizer *lpgr 
= [[UILongPressGestureRecognizer alloc] 
    initWithTarget:self action:@selector(handleLongPress:)]; 
lpgr.minimumPressDuration = .8; //seconds 
lpgr.delegate = self; 
[self.collectionView addGestureRecognizer:lpgr]; 
// Make the default gesture recognizer wait until the custom one fails. 
for (UIGestureRecognizer* aRecognizer in [self.collectionView gestureRecognizers]) { 
    if ([aRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) 
     [aRecognizer requireGestureRecognizerToFail:lpgr]; 
} 

现在。 在这种情况下,我得到一个崩溃,如果我加载应用程序,并去VC2,从来没有击中VC1这从来没有发生。因此失去了宝贵的时间搞清楚如何调试这后,我跑了仪器的僵尸,发现该行

[aRecognizer requireGestureRecognizerToFail:lpgr]; 

是问题,虽然这条线是在

viewdidload 
VC1的

,如果我从VC1移动到VC2这被称为!

我禁用了该段,我的代码工作正常,我只是不知道为什么会发生这种情况!

回答

0

您不应该创建一个依赖关系来自己对象。 由于您已经将手势识别器lpgr添加到collectionview,因此在for循环中,可能会创建lpgr本身的错误依赖关系。

将其改写成这样。

// Make the default gesture recognizer wait until the custom one fails. 
for (UIGestureRecognizer* aRecognizer in [self.collectionView gestureRecognizers]) { 
    if ([aRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) 
     [aRecognizer requireGestureRecognizerToFail:lpgr]; 
} 

[self.collectionView addGestureRecognizer:lpgr];