2014-12-30 52 views
0

我有一个全屏水平collectionview。如何将手势识别器添加到collectionview单元格中的uiview中

  1. 滚动禁用
  2. 分页被启用,也有对未来分页/分组按钮。

在我的collectionview cell中,我有一个标签,当用户向左/向右滑动时,我想识别它。 我将标记添加到标签后没有任何反应。

CODE:

collectionViewCell:

func addGesture(){ 
    let right = UISwipeGestureRecognizer(target: myLabel, action: "test") 
    right.direction = UISwipeGestureRecognizerDirection.Left 
    answer.addGestureRecognizer(right) 
} 

视图控制器:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("formQuestionCell", forIndexPath: indexPath) as QuestionCell 

    cell.addGesture() 

    return cell 
} 

我也尝试从myLabel切换目标自我,它仍然无法正常工作。

感谢

回答

0

我的addGesture代码移到视图控制器,这样就可以在视图控制器处理挥笔为好。

变化

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("formQuestionCell", forIndexPath: indexPath) as QuestionCell 

    let right = UISwipeGestureRecognizer(target: self, action: Selector("test:")) 
    right.direction = UISwipeGestureRecognizerDirection.Left 
    cell.answer.addGestureRecognizer(right) // I am assuming 'answer' is an outlet to the label you want to add a gesture recognizer to in the QuestionCell class 

    return cell 
} 

,然后你需要在视图控制器实现test:以及(因为你的目标设定为self):

func test(gestureRecognizer: UISwipeGestureRecognizer) { 
    // Deal with swipe 
} 
+0

谢谢, 我得到一个无法识别的选择器发送到实例。 – ilan

+0

您是否在视图控制器中实现了'test:'?你需要否则你会得到这样的错误。无论您设置了“目标”,都必须实现该选择器。 – trevorj

+0

效果很好,忘了补充:去测试。 – ilan

0
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] 
             initWithTarget:self action:@selector(clickEventOnImage:)]; 

[tapRecognizer setNumberOfTapsRequired:1]; 
[tapRecognizer setDelegate: self]; 
cell.uiviewelement.userInteractionEnabled = YES; 
[cell.uivielement addGestureRecognizer:tapRecognizer]; 
相关问题