2014-12-30 15 views
0

전 화면 가로 뷰가 있습니다.collectionview 셀에서 uiview에 제스처 인식기를 추가하는 방법

  1. 스크롤은 페이징에 대한 다음/이전 버튼이 있습니다,
  2. 페이징이 사용되지 않습니다.

내 collectionview 셀에는 사용자가 왼쪽/오른쪽으로 스 와이프 할 때 인식 할 레이블이 있습니다. 제스처를 레이블에 추가 한 후에 아무 일도 일어나지 않습니다.

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 
} 

와 당신이 (당신이 self로 목표를 설정하기 때문에)뿐만 아니라 뷰 컨트롤러에 test:을 구현해야합니다

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

감사합니다 , 인스턴스로 인식 할 수없는 선택기를 보냈습니다. – ilan

+0

뷰 컨트롤러에도'test :'를 구현 했습니까? 그렇지 않으면 오류가 발생합니다. 무엇을 설정하든'target'은 그 selector를 구현해야합니다. – 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]; 
관련 문제