2016-12-05 5 views
8

हैलो मेरे पास UICollectionView क्षैतिज छवि प्रविष्टि कोड है, मैं छवियों को स्क्रॉल करते समय PageControl जोड़ना चाहता हूं, मैंने पेजcontrol चयनकर्ता और IBOutlet जोड़ा, लेकिन, मैं इसे कैसे एकीकृत कर सकता हूं? के बीच। ?UICollectionView छवि स्क्रॉलिंग के अंदर पेजकंट्रोल कैसे जोड़ें

नीचे दिए गए मेरे कोड।

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { 


    @IBOutlet weak var View : DesignableView! 


    @IBOutlet var collectionView: UICollectionView! 
    @IBOutlet var collectionViewLayout: UICollectionViewFlowLayout! 

    @IBOutlet open weak var pageControl: UIPageControl? { 
     didSet { 
      pageControl?.addTarget(self, action: #selector(ViewController.pageChanged(_:)), for: .valueChanged) 
     } 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     pageControl?.numberOfPages = 11 


    } 


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 11; 
    } 




    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell: ImageCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCollectionViewCell", for: indexPath) as! ImageCollectionViewCell 
     cell.label.text = "Cell \(indexPath.row)" 
     cell.backgroundImageView.image = UIImage(named: "Parallax \(indexPath.row + 1)") 

     // Parallax cell setup 
     cell.parallaxTheImageViewScrollOffset(self.collectionView.contentOffset, scrollDirection: self.collectionViewLayout.scrollDirection) 
     return cell 
    } 



    @IBAction open func pageChanged(_ sender: UIPageControl) { 

     print(sender.currentPage) 


    } 




    // MARK: Delegate 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     return collectionView.bounds.size; 
    } 

    // MARK: Scrolling 

    func scrollViewDidScroll(_ scrollView: UIScrollView) { 
     // Parallax visible cells 
     for cell: ImageCollectionViewCell in collectionView.visibleCells as! [ImageCollectionViewCell] { 
      cell.parallaxTheImageViewScrollOffset(self.collectionView.contentOffset, scrollDirection: self.collectionViewLayout.scrollDirection) 
     } 
    } 


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



} 

उत्तर

22

यहां आपके पास क्षैतिज संग्रह दृश्य में पृष्ठ नियंत्रण पृष्ठांकन के साथ एक पूर्ण श्रेणी है। यह अभी मेरे अनुप्रयोगों में से एक पर काम कर रहा है, और सही तरीके से काम कर रहा है। यदि आप इसे काम नहीं कर पा रहे हैं, तो मुझसे पूछने में संकोच न करें और मैं आपकी मदद करूंगा।

पहले, स्टोरीबोर्ड में आप के साथ अपने CollectionView स्थापित करने के लिए है:

लेआउट: प्रवाह
स्क्रॉल दिशा: क्षैतिज
स्क्रॉल सक्षम
पेजिंग सक्षम

@IBOutlet weak var collectionView: UICollectionView! //img: 77 

@IBOutlet weak var pageControl: UIPageControl! 

var thisWidth:CGFloat = 0 

override func awakeFromNib() { 
    super.awakeFromNib() 

    thisWidth = CGFloat(self.frame.width) 
    collectionView.delegate = self 
    collectionView.dataSource = self 

    pageControl.hidesForSinglePage = true 

} 

func numberOfSections(in collectionView: UICollectionView) -> Int { 
    return 10 
} 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 1 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YourCell", for: indexPath) 

    return cell 
} 

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { 
    self.pageControl.currentPage = indexPath.section 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    thisWidth = CGFloat(self.frame.width) 
    return CGSize(width: thisWidth, height: self.frame.height) 
} 
+0

दोस्त pagecontrol साथ काम न जब मैं समारोह collectionView (_ collectionView: UICollectionView, numberOfItemsInSection अनुभाग: इंट) जोड़ा -> इंट { वापसी आत्म .myarray.count // यहां पृष्ठ नियंत्रण नहीं है } – SwiftDeveloper

+1

ऐसा इसलिए है क्योंकि प्रत्येक आइटम के लिए पृष्ठ बनाने के लिए, आपको पंक्तियों का उपयोग करना होगा, पंक्तियों –

+0

पर नहीं, लेकिन इस समाधान के साथ, जब उपयोगकर्ता अगले पृष्ठ पर स्वाइप करता है और फिर अपनी उंगली उठाए बिना फिर से वापस स्वाइप करता है, पेज नियंत्रण गलत पृष्ठ दिखाता है। मुझे लगता है कि आपको @ ल्यूक के उत्तर – gasparuff

22

मैं स्वीकृत उत्तर का प्रशंसक नहीं हूं। समय-समय पर, willDisplay cell उपयोगकर्ता इंटरैक्शन के आधार पर पृष्ठ नियंत्रण के लिए गलत पृष्ठ लौटाएगा।

मैं क्या का उपयोग करें:

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { 
    pageControl.currentPage = Int(scrollView.contentOffset.x)/Int(scrollView.frame.width) 
} 

इस पृष्ठ के बाद उपयोगकर्ता इस प्रकार स्क्रॉल pageControl की currentPage अधिक सटीक बनाने समाप्त हो गया है अद्यतन करेगा।

+0

यह विलडिस्प्लेसेल को बदलने के लिए बहुत अच्छा काम करता है .. –

3

scrollViewDidEndDecelerating के लिए प्रतीक्षा UIPageControl के धीमे अपडेट में परिणाम। यदि आप अधिक उत्तरदायी यूआई चाहते हैं, तो मैं सुझाव देता हूं कि scrollViewWillEndDragging(_:withVelocity:targetContentOffset:) लागू करें।

स्विफ्ट 4 उदाहरण है, जहां प्रत्येक संग्रह दृश्य अनुभाग एक पृष्ठ है:

override func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) { 
    if let collectionView = scrollView as? ScoreCollectionView, 
     let section = collectionView.indexPathForItem(at: targetContentOffset.pointee)?.section { 
     self.pageControl.currentPage = section 
    } 
} 
+1

यह एकमात्र स्वीकार्य उत्तर होना चाहिए क्योंकि यह करने के लिए यह सबसे साफ और उत्तरदायी तरीका है। – Axy

+0

मुझे 'item' – pixelfreak

+1

के लिए 'सेक्शन' को प्रतिस्थापित करना होगा यदि आप पर्याप्त तेज़ी से स्वाइप करते हैं, तो वर्तमान पृष्ठ गलत परिणाम उत्पन्न कर सकता है। – pixelfreak

संबंधित मुद्दे