gpt4 book ai didi

ios - 通过滑动显示单个单元格的 UICollectionView 来触发函数

转载 作者:行者123 更新时间:2023-11-30 13:40:14 24 4
gpt4 key购买 nike

我目前正在制作 Pokedex 应用程序,这是我的第一个应用程序。

这是我正在使用的 View 的屏幕截图。 http://imgur.com/aE2g4kL

屏幕截图是在滑动过程中截取的,以显示我想要如何触发 UI 更新。

带有 Pokemon 图片的顶部栏是一个 UICollectionView,它显示单个 UICollectionViewCell。我想触发一个函数,当图片离开屏幕一半时将 UI 更新为下一个 Pokemon。

我尝试过以下方法:

  • func UICollectionView didEndDisplayingCell

  • func UICollectionView cellForItemAtIndexPath

  • func UICollectionView willDisplayCell

这些的问题在于它们触发得太晚或太早。

我还尝试使用带有scrollViewDidScroll的内容偏移:

func scrollViewDidScroll(scrollView: UIScrollView) {

let bounds = UIScreen.mainScreen().bounds
let width = bounds.size.width
let offset = self.collectionView.contentOffset.x

var pokeID = Int((offset-width/2)/width+2) {
didSet {
print("Change Detected")
}
}
print(offset)
print(pokeID)
}

此代码打印正确的 Pokemon。我尝试使用 willSet 和 didSet 来触发“pokeID”中的更改,但没有任何运气。有人对我如何实现这一目标有任何想法吗?

最佳答案

听起来你想要这样的东西:

private var currentPokeID: Int = -1 {
didSet {
if currentPokeID != oldValue {
updateViews()
}
}
}

func scrollViewDidScroll(scrollView: UIScrollView) {
let bounds = self.collectionView.bounds
let midpoint = CGPointMake(bounds.midX, bounds.midY)
if let indexPath = self.collectionView.indexPathForItemAtPoint(midpoint) {
currentPokeID = indexPath.item
}
}

关于ios - 通过滑动显示单个单元格的 UICollectionView 来触发函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35662381/

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