2016-07-13 48 views
5

Ho una collezioneView con scorrimento verticale, che copre l'intero schermo del dispositivo (cioè schermo intero).Scorri verso sinistra/destra sulla raccoltaVisualizzazione non chiamata mentre lo scorrimento verticale

Ho registrato i gesti Swipe Left and Right per la visualizzazione della raccolta.

//------------right swipe gestures in collectionView--------------// 
    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.rightSwiped)) 
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right 
    self.collectionView.addGestureRecognizer(swipeRight) 

    //-----------left swipe gestures in collectionView--------------// 
    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.leftSwiped)) 
    swipeLeft.direction = UISwipeGestureRecognizerDirection.Left 
    self.collectionView.addGestureRecognizer(swipeLeft) 

Problema: Swipe a sinistra ea destra gesti callback non scatta mentre CollectionView scorre verticalmente.

C'è qualche soluzione semplice per questo.

qui è tutta la mia ViewController Classe

import UIKit 

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { 




@IBOutlet weak var collectionView: UICollectionView! 

let reuseIdentifier = "cell" // also enter this string as the cell identifier in the storyboard 
var items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48"] 


override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    collectionView.dataSource = self 
    collectionView.delegate = self 


    //------------right swipe gestures in collectionView--------------// 
    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.rightSwiped)) 
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right 
    self.collectionView.addGestureRecognizer(swipeRight) 

    //-----------left swipe gestures in collectionView--------------// 
    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.leftSwiped)) 
    swipeLeft.direction = UISwipeGestureRecognizerDirection.Left 
    self.collectionView.addGestureRecognizer(swipeLeft) 


} 


func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return self.items.count 
} 

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

    // get a reference to our storyboard cell 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CustomCell 

    // Use the outlet in our custom class to get a reference to the UILabel in the cell 
    cell.lable.text = self.items[indexPath.item] 
    cell.backgroundColor = UIColor.yellowColor() 

    return cell 
} 

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    print(indexPath.row) 
} 

func rightSwiped() 
{ 

    print("right swiped ") 
} 

func leftSwiped() 
{ 

    print("left swiped ") 

} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


} 

qui è la mia CollectionView assomigliare

enter image description here

EDIT 1

risolto, per le soluzioni di cliccare here

+0

Risolto .. Solutions è la seguente come la mia risposta –

risposta

2

La delegate di riconoscitori gesto predefinite della UICollcetionView è vista raccolta oggetto stesso (ovviamente).

L'implementazione predefinita di -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer restituisce YES nella classe UICollectionView.

Quindi, per risolvere il problema, è necessario impostare l'oggetto vista raccolta come delegato per i riconoscitori di gesti di scorrimento "sinistro" e "destro" come segue.

swipeRight.delegate = collectionView; 
swipeLeft.delegate = collectionView; 

Questo dovrebbe rendere la vostra rightSwiped() e leftSwiped() essere licenziato quando si verifica corrispondente colpo.

+0

Emmm, scusa non ho capito. Devo fare UIcollectionView personalizzato per fare questo? –

+0

grazie per avermi indicato dovrebbe Riconoscere simultaneamenteWithGestureRecognizer. L'ho fatto :) Pubblicheremo presto la mia soluzione –

+0

Ottimo. Grazie per aver accettato la mia risposta :). Ho pensato di menzionare la sottoclasse di UICollectionView se hai bisogno di un comportamento aggiuntivo. Tuttavia, il tuo attuale ambito di requisiti (solo facendo funzionare i gesti) non richiede una sottoclasse. – Hariprasad

0

Provare il seguente codice potrebbe esserti d'aiuto.

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

    // get a reference to our storyboard cell 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CustomCell 

    cell.lable.text = self.items[indexPath.item] 
    cell.backgroundColor = UIColor.yellowColor() 
    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.leftSwiped)) 
    swipeLeft.direction = UISwipeGestureRecognizerDirection.Left 
    cell.addGestureRecognizer(swipeLeft) 

    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.rightSwiped)) 
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right 
    cell.addGestureRecognizer(swipeRight) 

    return cell 
} 
+0

alcun effetto! Stesso comportamento con le celle –

0

qui è molto semplice soluzione

1) È necessario prendere una proprietà per archiviare il contenuto precedente compensato

2) Implementare il metodo delegato ScrollViewDidScroll & confrontare il contenuto attuale Offset con il contenuto precedente Offset

var contentOffset: CGFloat = 0 

// MARK: UICollectionViewDelegate

func scrollViewDidScroll(scrollView: UIScrollView) { 

    if contentOffset > scrollView.contentOffset.y { 
    // scrolling up 
    } else if contentOffset < scrollView.contentOffset.y { 
    //scrolling Down 
    } 
    contentOffset = scrollView.contentOffset.y 
} 

3) Può essere eseguito senza aggiungere alcun riconoscimento di gesture.

+0

Sto scorrendo verticalmente. non orizzontalmente. questo è valido per lo scroll orizzontale –

+0

quindi su e giù ... non a sinistra ea destra –

+0

sì sto scorrendo verticalmente con il gesto su/giù ma mentre scorro il dito verso sinistra/destra non viene attivato. questo è il problema –

0

Grazie @Hariprasad per indicare me shouldRecognizeSimultaneouslyWithGestureRecognizer

Ecco la soluzione

ho sottoclasse il UICollectionView e implementato il UIGestureRecognizerDelegate come qui di seguito

import UIKit 

class TouchCollectionView: UICollectionView, UIGestureRecognizerDelegate { 

/* 
// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
override func drawRect(rect: CGRect) { 
    // Drawing code 
} 
*/ 

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 

    let gesture = UIGestureRecognizer() 
    gesture.delegate = self // Set Gesture delegate so that shouldRecognizeSimultaneouslyWithGestureRecognizer can be set to true on initialzing the UICollectionView 
} 

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool { 
    return true 
} 
} 

intero ViewController rimarrà lo stesso come indicato nella domanda