简体   繁体   English

触摸时缩小 UICollectionViewCell - delaysContentTouches 问题

[英]Shrinking UICollectionViewCell on touch - delaysContentTouches issue

I have a scrollView, which contains modules with collectionView and modules with tableView.我有一个 scrollView,它包含带有 collectionView 的模块和带有 tableView 的模块。 So you can scroll up and down with scrollView, and scroll right and left in collectionView modules.所以可以用scrollView上下滚动,在collectionView模块里可以左右滚动。 After pressing on collectionView cell, user is navigated to another view controller.按下 collectionView 单元格后,用户将导航到另一个视图 controller。

here is an approximate sketch of how the UI looks like这是 UI 外观的大致草图

I need to make cells in collectionView shrink on touch.我需要让 collectionView 中的单元格在触摸时收缩。 The problem is, that when I try to do this - I have a slight delay between touch and the start of shrinking.问题是,当我尝试这样做时 - 我在触摸和开始收缩之间有轻微的延迟。 I am using this code to make the cell shrink:我正在使用此代码使单元格缩小:

 func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
    if let cell = collectionView.cellForItem(at: indexPath) {
        UIView.animate(withDuration: 0.1) {
            cell.transform = CGAffineTransform(scaleX: 0.95, y: 0.95)
        }
    }
}

I have learned that the delay happens because delaysContentTouches property is automatically set to true in UICollectionViews, in order to detect properly scrolling and selecting touches of user.我了解到延迟发生是因为delaysContentTouches属性在 UICollectionViews 中自动设置为true ,以便检测正确滚动和选择用户的触摸。 So I have set it to false , and also since collectionView was located inside the scrollView as I mentioned above - I had to also set delaysContentTouches to false in the scrollView in order to remove the delay between touch and shrinking for collectionView cells.所以我将它设置为false ,而且由于如上所述 collectionView 位于 scrollView 内部 - 我还必须在 scrollView 中将delaysContentTouches设置为 false 以消除 collectionView 单元格的触摸和收缩之间的延迟。

The problem is that when I set delaysContentTouches to false in scrollView - it also automatically applies to tableView, and cells inside tableView also begin highlighting immediately when you're just scrolling up and down, which is definitely undesired behaviour in this case.问题是,当我在 scrollView 中将delaysContentTouches设置为 false 时 - 它也会自动应用于 tableView,并且当您只是上下滚动时,tableView 内的单元格也会立即开始突出显示,这在这种情况下绝对是不受欢迎的行为。

I have tried setting delaysContentTouches in tableView to true , but this did not help to prevent this behaviour.我尝试将 tableView 中的delaysContentTouches设置为true ,但这无助于防止这种行为。

Is there any way to make my cells shrinking immediately only for collectionView, without touching the functionality of tableView highlighting in this case?在这种情况下,有什么方法可以让我的单元格仅针对 collectionView 立即收缩,而不会触及 tableView 突出显示的功能? I am quite new to this so would appreciate any advice.我对此很陌生,因此不胜感激任何建议。

Not sure if this helps or not, but why aren't you using UICollectionView's didSelectItemAt instead of didHighlightItemAt ?不确定这是否有帮助,但为什么不使用 UICollectionView 的didSelectItemAt而不是didHighlightItemAt

That may be another reason for the delay.这可能是延迟的另一个原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM