[英]how to add a gesture recogniser to a uiview in a collectionview cell
我有一個全屏的水平collectionview。
在我的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切換到self,但仍然無法正常工作。
謝謝
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(clickEventOnImage:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate: self];
cell.uiviewelement.userInteractionEnabled = YES;
[cell.uivielement addGestureRecognizer:tapRecognizer];
我會將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:
因為將target設置為self
):
func test(gestureRecognizer: UISwipeGestureRecognizer) {
// Deal with swipe
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.