目前我的收藏视图包含4个单元格。这是一个水平集合视图,每个显示一个单元格,因此每次有一个单元格应该显示自己。当我旋转我的设备时,每个屏幕模式的这一个单元格也打开。每当我说我的collectionView显示可见的单元格时,它会显示不同的数组。 (从纵向到水平2个单元格,从水平到纵向甚至是3个单元格的数组)。如果这个visibleCells数组每次都不同,为什么我们需要呢?为什么它每次都不同(我的意思是从portr到hor,反之亦然) 多数民众赞成我的代码
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(collectionView)
view.addSubview(pageControl)
view.addSubview(skipButton)
view.addSubview(nextButton)
print( view.constraints)
nextButton.anchorWithConstantsToTop(nil, left: nil, bottom: nil, right:
view.rightAnchor, topConstant: 16, leftConstant: 0, bottomConstant: 0, rightConstant: 0)
nextButtonTopAnchor = nextButton.topAnchor.constraint(equalTo: view.topAnchor, constant: 16)
nextButtonTopAnchor?.isActive = true
nextButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
nextButton.widthAnchor.constraint(equalToConstant: 60).isActive = true
skipButton.anchorWithConstantsToTop(nil, left: view.leftAnchor, bottom: nil, right: nil, topConstant: 16, leftConstant: 0, bottomConstant: 0, rightConstant: 0)
skipButtonTopAnchor = skipButton.topAnchor.constraint(equalTo: view.topAnchor, constant: 16)
skipButtonTopAnchor?.isActive = true
skipButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
skipButton.widthAnchor.constraint(equalToConstant: 60).isActive = true
pageControl.anchorWithConstantsToTop(nil, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, topConstant: 0, leftConstant: 28, bottomConstant: 0, rightConstant: 28)
print("\(pageControlConstant) L")
pageControl.heightAnchor.constraint(equalToConstant: 40).isActive = true
if UIDevice.current.orientation.isPortrait || (UIDevice.current.orientation.isFlat && UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ) { pageControlBottomAnchor = pageControl.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor, constant: 0)
} else if UIInterfaceOrientationIsPortrait(self.interfaceOrientation) == false {
pageControlBottomAnchor = pageControl.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor, constant: 5)
}
pageControlBottomAnchor?.isActive = true
collectionView.anchorToTop(view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor)
registerCells()
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
view.endEditing(true)
}
override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator)
{
if pageControl.currentPage != pages.count {
if UIDevice.current.orientation.isLandscape || (UIDevice.current.orientation.isFlat && UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
self.nextButtonTopAnchor?.constant = 0
self.skipButtonTopAnchor?.constant = 0
self.pageControlBottomAnchor?.constant = 5
self.tabBarController?.tabBar.isHidden = true
} else {
self.nextButtonTopAnchor?.constant = 16
self.skipButtonTopAnchor?.constant = 16
self.pageControlBottomAnchor?.constant = 0
self.tabBarController?.tabBar.isHidden = false
}
}
if pageControl.currentPage == pages.count {
var mySpecialCell = collectionView.cellForItem(at:IndexPath(item: pages.count, section: 0)) as! LoginCell
if (UIDevice.current.orientation.isLandscape || (UIDevice.current.orientation.isFlat && UIInterfaceOrientationIsLandscape(UIInterfaceOrientation.landscapeLeft)) )
{
mySpecialCell.someShit = "landSpace"
} else {
mySpecialCell.someShit = "portrait"
}
collectionView.reloadData()
}
collectionView.collectionViewLayout.invalidateLayout()
collectionView.reloadData()
DispatchQueue.main.async {
let indexPath = IndexPath(item: self.pageControl.currentPage, section: 0)
self.collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
if (UIDevice.current.orientation.isLandscape || (UIDevice.current.orientation.isFlat && UIInterfaceOrientationIsLandscape(UIInterfaceOrientation.landscapeLeft)) )
{
} else {
}
self.collectionView.reloadData()
}
}
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let pageNumber = Int(targetContentOffset.pointee.x / view.frame.width)
print( view.constraints)
pageControl.currentPage = Int(pageNumber)
if pageNumber == pages.count {
nextButtonTopAnchor?.constant = -40
skipButtonTopAnchor?.constant = -40
pageControlBottomAnchor?.constant = 50
print(pageControlBottomAnchor)
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
} else {
if UIDevice.current.orientation.isLandscape || (UIDevice.current.orientation.isFlat && UIInterfaceOrientationIsLandscape(self.interfaceOrientation) ) {
pageControlBottomAnchor?.constant = 5
self.nextButtonTopAnchor?.constant = 0
self.skipButtonTopAnchor?.constant = 0
} else if (UIDevice.current.orientation.isFlat && UIInterfaceOrientationIsPortrait(self.interfaceOrientation) || !UIDevice.current.orientation.isLandscape ) {
self.nextButtonTopAnchor?.constant = 16
self.skipButtonTopAnchor?.constant = 16
pageControlBottomAnchor?.constant = 0
}
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
}
fileprivate func moveControlConstraintsOffScreen() {
pageControlBottomAnchor?.constant = 150
pageControlBottomAnchor?.isActive = true
}
fileprivate func registerCells() {
collectionView.register(PageCell.self, forCellWithReuseIdentifier: cellId)
collectionView.register(LoginCell.self, forCellWithReuseIdentifier: loginCellId)
collectionView.register(LoginCellLandscape.self, forCellWithReuseIdentifier: loginCellLandscapeId)
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
print("I will display \(cell) for \(indexPath)")
}
func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
print("I did end displaying \(cell) for \(indexPath)")
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return pages.count + 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let loginCell = collectionView.dequeueReusableCell(withReuseIdentifier: loginCellId, for: indexPath)
return loginCell
}
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! PageCell
let page = pages[indexPath.item]
cell.page = page
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: view.frame.height)
}
}
答案 0 :(得分:2)
将问题视为询问为什么UICollectionView
具有visibleCells
功能......
听起来你不需要它。它在每个应用程序中都没用。但有时它是。这是一个例子:
假设您的单元格显示的数据代表了一段时间内的事件 - 也许是时间轴中的Twitter帖子。并假设您的UI包含一个字段,显示每个事件的年龄 - “20s ago”,“3m ago”等。为了保持这些时间准确,您可以设置计时器以定期更新值。计时器启动时会发生什么?
您可以告诉表视图重新加载其数据。这将是有效的,但它的工作量远远超过必要的。您无需重新加载整个单元格,只需更新单元格中的一个字段即可。但是哪个细胞?只有那些可见的。因此,请询问集合视图哪些单元格可见,并更新这些单元格中的时间字段。
当设备旋转时,询问哪些单元格可见是没有用的,因为正如您所观察到的那样,可见单元格会发生变化。不过,在我没想过的那种情况下,有人可能会使用它。