我有一个全屏水平收集视图。
在我的collectionview单元格中,我有一个标签,我想在用户向左/向右滑动时识别它。 在我向标签添加手势后,没有任何事情发生。
代码:
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 :(得分:0)
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(clickEventOnImage:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate: self];
cell.uiviewelement.userInteractionEnabled = YES;
[cell.uivielement addGestureRecognizer:tapRecognizer];
答案 1 :(得分: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
}