【问题标题】:How to reduce the amount of time between a tap on a cell and the running of certain code?如何减少点击单元格和运行某些代码之间的时间?
【发布时间】:2019-06-17 19:59:06
【问题描述】:

我有下面的代码,它成功地导致collectionView 中的单元格在被点击时被按下。

问题是,在萧条发生之前,人们必须坚持很长时间。不要误会我的意思。它只有大约 1 秒,但如果你将该速度与 snapchat 的速度进行比较,你可以清楚地看到 snapchat 几乎是即时的。

func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {

     UIView.animate(withDuration: 0.1, animations: {

         collectionView.cellForItem(at: indexPath)!.transform = CGAffineTransform.identity.scaledBy(x: 0.95, y: 0.95)

      })
 }

问题:我怎样才能为每个单元格制作这个动画,是即时的还是更接近它,​​就像 snapchat 一样?

更新:

这似乎是这样做的:

func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {

    UIView.animate(withDuration: 0.1, animations: {

        collectionView.cellForItem(at: indexPath)!.transform = CGAffineTransform.identity.scaledBy(x: 0.95, y: 0.95)

    }) { (true) in

        UIView.animate(withDuration: 0.1, animations: {

            collectionView.cellForItem(at: indexPath)!.transform = CGAffineTransform.identity.scaledBy(x: 1, y: 1)

        })

    }

}

如果有人想要一些基本的东西,这是一个选项。

【问题讨论】:

标签: ios swift uicollectionview cell tap


【解决方案1】:

在界面生成器中禁用collectionView 内容触摸延迟:

或在代码中:

myCollectionView.delaysContentTouches = false

提示:为此动画启用.beginFromCurrentState 选项将使它看起来更加流畅和响应迅速。来自 Apple 在beginFromCurrentState 上的文档:

从与已经进行中的动画关联的当前设置开始动画。

UIView.animate(withDuration: 0.1, delay: 0, options: [.beginFromCurrentState], animations: { 
}) { finished in 
}

【讨论】:

  • 这将是您提示的正确实现吗?:myCollectionView。 beginFromCurrentState = true //因为它说没有 .beginFromCurrentState
  • 不,它用于UIView.animate函数
【解决方案2】:

如果你想要即时性,你可以在你的自定义 UICollectionViewCell 的 touchesBegan 和 touchesEnded/touchesCanceled 方法中来回转换

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    • 2021-12-02
    • 1970-01-01
    • 2015-06-02
    相关资源
    最近更新 更多