gpt4 book ai didi

ios - 将 UIScrollView Delegate 和 UICollectionView Delegate 分配给同一个类

转载 作者:行者123 更新时间:2023-11-29 05:51:57 25 4
gpt4 key购买 nike

我有一个分页UIScrollView,每个页面都填充有不同的 View Controller 。 ScrollView 上方是一个 UICollectionView,充当菜单栏。当您滚动 ScrollView 页面时,菜单栏会稍微移动一点。您可以从左侧的 gif 中看到。

将他们的代表设置为不同的类可以使一切正常工作,如左侧的 gif 所示。但是,将它们设置为同一个类会扰乱 UICollectionView 的行为。

如何将他们的代表设置为同一类?

import UIKit

class MenuView: UIView, UICollectionViewDataSource {

let collcetionView: UICollectionView = {
let view = UICollectionView()
// Setup...
return view
}()

override init(frame: CGRect) {
super.init(frame: frame)
setupCollectionView()
collcetionView.dataSource = self
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

fileprivate func setupCollectionView() {
// Autolayout code...
}

// Datasource methods to populate collection view cells
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// Populate cell code...
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// Populate cell code...
}
}


class MainView: UIView {
// Contains paging scroll view and menu bar
var menu: MenuView!

let scrollView: UIScrollView = {
let view = UIScrollView()
// Setup...
return view
}()

override init(frame: CGRect) {
super.init(frame: frame)
setupMenu()
setupScrollView()

}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

fileprivate func setupScrollView() {
// Autolayout code...
}
fileprivate func setupMenu() {
menu = MenuView()
// Autolayout code...
}
}


class MainController: UIViewController, UIScrollViewDelegate, UICollectionViewDelegate {
var mainView: MainView!

override func loadView() {
super.loadView()
mainView = MainView()
view = mainView
}

override func viewDidLoad() {
super.viewDidLoad()
mainView.scrollView.delegate = self
mainView.menu.collcetionView.delegate = self // <<--- THIS IS WHAT BREAKS EVERYTHING
}


func scrollViewDidScroll(_ scrollView: UIScrollView) {

// Moving menu bar with page scroll
mainView.menu.collectionView.contentOffset = CGPoint(x: scrollView.contentOffset.x/SCROLL_FACTOR - (firstIndexPosition/SCROLL_FACTOR - difference/2), y: 0)



// Fade in and out highlighted state of menu bar cell
let exactPage = (scrollView.contentOffset.x / SCREEN_WIDTH)
let currentPage = (scrollView.contentOffset.x / SCREEN_WIDTH).rounded()
let unitExact = currentPage - exactPage
//print(String(format: "exact: %.2f, ", exactPage) + "current: \(currentPage), " + String(format: "unit: %.2f, ", unitExact))

if exactPage > currentPage {
// exact > current
// fade out/in left icon
// select current
let unit = 0 - unitExact // from 0 - 0.5
let cell = mainView.menu.collectionView.cellForItem(at: IndexPath(item: Int(currentPage), section: 0)) as! MenuBarCell
let mapped = unit.map(from: 0.0...0.5, to: 0...149.0)
print(cell)
setCellColor(cell: cell, value: mapped)
} else if exactPage < currentPage {
// exact < current
// fade out/in right icon
// select current
let unit = unitExact // from 0 - 0.5
let cell = mainView.menu.collectionView.cellForItem(at: IndexPath(item: Int(currentPage), section: 0)) as! MenuBarCell
let mapped = unit.map(from: 0.0...0.5, to: 0...149.0)
setCellColor(cell: cell, value: mapped)
} else if exactPage == currentPage {
// exact = current
// darken that icon
// select current
}
}
}

最佳答案

UICollectionViewUITableView继承自UIScrollView

如果您将 Collection View 和 ScrollView 的委托(delegate)设置为同一个类,则将为您的 Collection View 和 ScrollView 调用scrollViewDidScroll委托(delegate)方法。

您需要检查为什么调用 scrollViewDidScroll 并采取相应措施。

最简单的方法是一个 guard 语句,如果您感兴趣的 ScrollView 没有调用委托(delegate)方法,该语句就会返回。

如果您需要根据所涉及的 ScrollView 执行不同的代码,您可以使用一系列 if 语句或 switch 语句。

func scrollViewDidScroll(_ scrollView: UIScrollView) {

guard scrollView == self.scrollView else {
return
}

// Moving menu bar with page scroll
mainView.menu.collectionView.contentOffset = CGPoint(x: scrollView.contentOffset.x/SCROLL_FACTOR - (firstIndexPosition/SCROLL_FACTOR - difference/2), y: 0)



// Fade in and out highlighted state of menu bar cell
let exactPage = (scrollView.contentOffset.x / SCREEN_WIDTH)
let currentPage = (scrollView.contentOffset.x / SCREEN_WIDTH).rounded()
let unitExact = currentPage - exactPage
//print(String(format: "exact: %.2f, ", exactPage) + "current: \(currentPage), " + String(format: "unit: %.2f, ", unitExact))

if exactPage > currentPage {
// exact > current
// fade out/in left icon
// select current
let unit = 0 - unitExact // from 0 - 0.5
let cell = mainView.menu.collectionView.cellForItem(at: IndexPath(item: Int(currentPage), section: 0)) as! MenuBarCell
let mapped = unit.map(from: 0.0...0.5, to: 0...149.0)
print(cell)
setCellColor(cell: cell, value: mapped)
} else if exactPage < currentPage {
// exact < current
// fade out/in right icon
// select current
let unit = unitExact // from 0 - 0.5
let cell = mainView.menu.collectionView.cellForItem(at: IndexPath(item: Int(currentPage), section: 0)) as! MenuBarCell
let mapped = unit.map(from: 0.0...0.5, to: 0...149.0)
setCellColor(cell: cell, value: mapped)
} else if exactPage == currentPage {
// exact = current
// darken that icon
// select current
}
}

关于ios - 将 UIScrollView Delegate 和 UICollectionView Delegate 分配给同一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55581709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com