我在一个屏幕上有一个滚动视图和一个集合视图..滚动视图有一个页面控件,而集合视图没有一个页面控件..
问题是当我在集合视图中滚动时..滚动视图的页面控件更改了其当前页面!
如何停止?并使页面控件仅在我在scrollview中滚动时才更改?
这是我的代码:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "homenewscell", for: indexPath as IndexPath) as! HomeNewsCollectionViewCell
cell.date.text = dates[indexPath.item]
cell.img.image = UIImage(named: images[indexPath.item])
cell.title.text = titles[indexPath.item]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = newsSlide.cellForItem(at: indexPath) as! HomeNewsCollectionViewCell
self.newsId = cell.id.text!
DispatchQueue.main.async {
self.performSegue(withIdentifier: "news", sender: self)
}
}
override func viewDidLoad() {
super.viewDidLoad()
newsSlide.delegate = self
newsSlide.dataSource = self
slides = createSlides()
setupSlideScrollView(slides: slides)
pageControl.numberOfPages = slides.count
pageControl.currentPage = 0
view.bringSubviewToFront(pageControl)
}
func createSlides() -> [banner] {
let slide1:banner = Bundle.main.loadNibNamed("banner", owner: self, options: nil)?.first as! banner
print(banners[0].image.src)
slide1.img.downloaded(from:banners[0].image.src)
slide1.clipsToBounds = true
//slide1.img.image = UIImage(named: "bannerex")
slide1.img.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapped)))
let slide2:banner = Bundle.main.loadNibNamed("banner", owner: self, options: nil)?.first as! banner
slide2.img.downloaded(from:banners[1].image.src)
//slide2.img.image = UIImage(named: "bannerex")
slide2.clipsToBounds = true
slide2.img.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapped)))
let slide3:banner = Bundle.main.loadNibNamed("banner", owner: self, options: nil)?.first as! banner
slide3.img.downloaded(from:banners[2].image.src)
//slide3.img.image = UIImage(named: "bannerex")
slide3.clipsToBounds = true
slide3.img.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapped)))
return [slide1, slide2, slide3]
}
@objc func tapped(_ sender: UITapGestureRecognizer){
print("tapped")
print(pageControl.currentPage)
let lang = MOLHLanguage.isArabic() ? "ar" : "en"
UIApplication.shared.open(URL(string: "\(APIManager.APIList.baseUrl + lang + banners[pageControl.currentPage].link)")!)
}
func setupSlideScrollView(slides : [banner]) {
scrollView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
scrollView.contentSize = CGSize(width: view.frame.width * CGFloat(slides.count), height: view.frame.height)
scrollView.isPagingEnabled = true
for i in 0 ..< slides.count {
slides[i].frame = CGRect(x: view.frame.width * CGFloat(i), y: 0, width: view.frame.width, height: view.frame.height)
scrollView.addSubview(slides[i])
}
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let pageIndex = round(scrollView.contentOffset.x/view.frame.width)
pageControl.currentPage = Int(pageIndex)
}
如何解决这个问题?
答案 0 :(得分:2)
您可以尝试
variable
或
func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard scrollView != collectionView else { return }
let pageIndex = round(scrollView.contentOffset.x/view.frame.width)
pageControl.currentPage = Int(pageIndex)
}