我认为实现它的方法是使用 UIView.animate (...)。但是让我卡住的部分是关于如何添加手势识别器来检测触摸。我尝试使用 UILongPressGestureRecognizer,但这会导致用户无法滚动单元格。此处的任何帮助将不胜感激。
让我更清楚地解释一下我的问题,因为这里的一些用户给了我不相关的答案。请引用 iOS 12 ShortCut App 并查看用户在图库选项卡中滚动浏览单元格时的单元格收缩效果
下面是我尝试在 didHighlight 函数中做动画。虽然它可以工作,但是在点击单元格和单元格开始动画之间有轻微的延迟(大约 0.1 秒到 0.3 秒),不像 ShortCut 应用程序,单元格几乎立即收缩并且感觉更自然
func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
UIView.animate(withDuration: 0.1) {
cell?.transform = CGAffineTransform(scaleX: 0.95, y: 0.95)
}
}
func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
UIView.animate(withDuration: 0.1) {
cell?.transform = CGAffineTransform(scaleX: 1, y: 1)
}
}
最佳答案
我做过类似的事情,但将动画放在 didSelectItemAt:
方法中,而不是突出显示方法。我转至另一个 View ,因此将转场放在闭包的完成部分。
UIView.animate(withDuration: 0.1, delay: 0, options: [.curveEaseOut], animations: {
cell.transform = CGAffineTransform(scaleX: 0.95, y: 0.95)
}) { finished in
UIView.animate(withDuration: 0.1, delay: 0, options: [.curveEaseIn], animations: {
cell.transform = CGAffineTransform.identity
}) { finished in
self.performSegue(withIdentifier: "YOUR-SEGUE-HERE", sender: indexPath)
}
}
编辑:抱歉。我想我正在读我想读的东西。我看了一下应用程序,我认为您需要通过将以下内容放入 UICollectionViewCell 子类来执行一些操作,例如捕捉触摸。显然,您需要针对您的特定用例进行更改。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
UIView.animate(withDuration: 0.1) {
self.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
UIView.animate(withDuration: 0.1) {
self.transform = CGAffineTransform(scaleX: 1, y: 1)
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
UIView.animate(withDuration: 0.1) {
self.transform = CGAffineTransform(scaleX: 1, y: 1)
}
}
关于ios - Swift - 在 UICollectionView 中点击时如何使单元格缩小效果(如 ios 12 快捷方式应用程序和 App Store 应用程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52650562/