2016-07-13 17 views
5

Tôi có một bộ sưu tậpXem với cuộn dọc, bao phủ toàn bộ màn hình trên thiết bị (tức là toàn màn hình).Vuốt sang trái/phải trên bộ sưu tập Xem không được gọi khi di chuyển dọc

Tôi đã đăng ký các cử chỉ Swipe Left and Right cho chế độ xem bộ sưu tập của mình.

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

Vấn đề: Swipe trái và cử chỉ đúng callback không cháy, trong khi collectionView đang di chuyển theo chiều dọc.

Có cách giải quyết đơn giản nào cho việc này không.

đây là toàn bộ ViewController tôi Lớp

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


} 

đây là collectionView của tôi trông giống như

enter image description here

EDIT 1

Giải Quyết, các giải pháp kích here

+0

Giải Quyết .. Giải pháp được cung cấp dưới đây là câu trả lời của tôi –

Trả lời

2

Các delegate của recognisers cử chỉ mặc định của UICollcetionView là xem bộ sưu tập đối tượng chính nó (rõ ràng).

Triển khai mặc định -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer trả về YES ở bên cạnh lớp UICollectionView.

Vì vậy, để giải quyết vấn đề của bạn, bạn cần đặt đối tượng chế độ xem bộ sưu tập làm đại biểu cho trình nhận dạng cử chỉ vuốt "trái" và "phải" của bạn như sau.

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

này sẽ làm cho bạn rightSwiped()leftSwiped() để bị sa thải khi swipe tương ứng xảy ra.

+0

Emmm, Xin lỗi tôi không nhận được điều này. Tôi có phải tạo giao diện người dùng tùy chỉnh để thực hiện việc này không? –

+0

cảm ơn vì đã chỉ cho tôi shouldRecognizeSimultaneouslyWithGestureRecognizer. Tôi đã làm điều đó :) Sẽ đăng giải pháp của tôi sớm –

+0

Tuyệt vời. Cảm ơn bạn đã chấp nhận câu trả lời của tôi :). Tôi đã đề cập đến việc phân lớp UICollectionView nếu bạn cần thêm hành vi. Tuy nhiên, phạm vi yêu cầu hiện tại của bạn (chỉ cần thực hiện các thao tác cử chỉ) không yêu cầu phân lớp phụ. – Hariprasad

0

Hãy thử mã sau nó có thể giúp bạn.

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

không ảnh hưởng! cùng một hành vi với các tế bào –

0

Dưới đây là rất đơn giản Giải pháp

1) Bạn cần có một tài sản để lưu trữ nội dung trước đó bù đắp

2) Thực hiện các phương pháp đại biểu ScrollViewDidScroll & so sánh nội dung hiện tại offset với nội dung trước đó 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) Có thể thực hiện được mà không cần thêm bất kỳ trình nhận dạng cử chỉ nào.

+0

Tôi đang cuộn theo chiều dọc. không theo chiều ngang. điều này có giá trị cho cuộn ngang –

+0

thì nó sẽ lên & xuống ..không sang trái & phải –

+0

có Tôi đang cuộn theo chiều dọc với cử chỉ lên/xuống nhưng trong khi cuộn trượt sang trái/Phải không được bắn. đó chính là vấn đề –

0

Cảm ơn @Hariprasad đã chỉ cho tôi shouldRecognizeSimultaneouslyWithGestureRecognizer

Dưới đây là giải pháp

Tôi có phân lớp các UICollectionView và thực hiện các UIGestureRecognizerDelegate như dưới đây

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

Tổng ViewController sẽ vẫn như cũ như đã đề cập trong câu hỏi

Các vấn đề liên quan