2016-07-13 5 views
5

コレクションビューは垂直スクロールで、デバイスの全画面(全画面)をカバーしています。コレクションを左右にスワイプします。スクロール時に垂直方向にスクロールしません。

私はコレクションビューのためにSwipe Left and Rightジェスチャーを登録しています。

//------------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) 

問題:左 スワイプ、右のジェスチャーコールバックcollectionViewが縦にスクロールしている間に発生しません。

これには簡単な回避策がありますか?ここ

は私collectionViewをクリックソリューションのために、解決

enter image description here

EDIT 1

のように見えている私の全体のViewControllerクラスここ

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. 
} 


} 

あるhere

+0

解決..ソリューションは、質問で述べたように全体のViewControllerは同じままになりますが私の答え –

答えて

2

UICollcetionViewのデフォルトのジェスチャーrecognisersのdelegateはコレクションビューオブジェクト自体(明らかに)です。

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizerのデフォルト実装では、UICollectionViewクラスのサイドにYESが返されます。

あなたの問題に対処するには、以下のように、コレクションビューオブジェクトを代理人として左および右のスワイプジェスチャ認識子に設定する必要があります。

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

これは、対応するスワイプが発生したときに、あなたのrightSwiped()leftSwiped()は解雇することにする必要があります。あなたが以前のコンテンツを格納するプロパティを取る必要がある。ここ

+0

Emmm、申し訳ありません、私はこれを得ていません。これを行うにはカスタムUIcollectionViewを作成する必要がありますか? –

+0

私は指すためにありがとうshouldRecognizeSimultaneouslyWithGestureRecognizer。私はそれを行っている:) は私の答えを受け入れるため:)すぐ –

+0

グレート.Thanksを私の解決策を掲載します。あなたが要件のあなたの現在のスコープbehaviour.However追加の必要がある場合、私はサブクラス化を必要としない(ただジェスチャーが動作すること)UICollectionViewをサブクラス化について言及すると思いました。 – Hariprasad

0

次のコードを試してみてください。

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

として以下に提供される一切影響しません!細胞と同じ振る舞い –

0

非常にシンプルなソリューションが

1である)が

2をオフセット)デリゲートメソッドScrollViewDidScroll &を実装する前のコンテンツで現在のオフセット量を比較

var contentOffset: CGFloat = 0 
オフセット

//マーク: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)ジェスチャ認識機能を追加することなく実行できます。

+0

を掲載しました。水平ではありません。 これは右解雇ではない/左水平スクロールのため –

+0

が、それはアップ&ダウン..not左&右 –

+0

ますはい、私はアップ/ダウンジェスチャーで垂直スクロールが、私のスワイプをスクロールしながら午前有効です。それは問題です –

0

ここで私にshouldRecognizeSimultaneouslyWithGestureRecognizer

を指してくれてありがとう@Hariprasadが解決

私はUICollectionViewをサブクラス化し、UIGestureRecognizerDelegate

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 
} 
} 
  • 以下のような変更をあなたのcollectionViewのクラスプロパティを実装しているありますアイデンティティ・インスペクタから http://prntscr.com/bsqbq3

関連する問題