gpt4 book ai didi

ios - 如何在collectionview中添加类似pickerview的声音反馈?

转载 作者:行者123 更新时间:2023-11-28 11:33:32 25 4
gpt4 key购买 nike

您好,我正在尝试在滚动浏览 collectionview 项目时添加反馈。我应该在 collectionview 委托(delegate)中的何处添加反馈代码。如果我添加 willDisplay 然后添加最初将显示的单元格将调用不好的反馈。我只需要在用户滚动并选择项目时提供反馈。

最佳答案

假设您只在一个方向(如垂直方向)滚动并且所有项目行具有相同的高度,您可以使用 scrollViewDidScroll(_:) 来检测选择,例如 UIPickerView。

class ViewController {
var lastOffsetWithSound: CGFloat = 0
}

extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if let flowLayout = ((scrollView as? UICollectionView)?.collectionViewLayout as? UICollectionViewFlowLayout) {
let lineHeight = flowLayout.itemSize.height + flowLayout.minimumLineSpacing
let offset = scrollView.contentOffset.y
let roundedOffset = offset - offset.truncatingRemainder(dividingBy: lineHeight)
if abs(lastOffsetWithSound - roundedOffset) > lineHeight {
lastOffsetWithSound = roundedOffset
print("play sound feedback here")
}
}
}
}

请记住,UICollectionViewDelegateFlowLayout 继承了 UICollectionViewDelegate,而后者又继承了 UIScrollViewDelegate,因此您可以在任何位置声明 scrollViewDidScroll他们。

关于ios - 如何在collectionview中添加类似pickerview的声音反馈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56239357/

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