如何解决轻按 Swift 动画 CollectionView 项目
我试图在 CollectionViewCell
时为 tapped
设置动画,有点像在 AppStore 中。我几乎得到了这个行为,但它不是 100% 流畅:
这里有一个 video 以便更好地理解。
如果您仔细观察,您会发现在点击 cell
后,它再次缩小片刻。
这是我的代码:
func shrink(down: Bool) {
UIView.animate(withDuration: 0.2) {
if down {
self.theView.transform = CGAffineTransform(scaleX: 0.95,y: 0.95)
} else {
self.theView.transform = .identity
}
}
}
override var isHighlighted: Bool {
didSet {
shrink(down: isHighlighted)
}
}
如何解决商品只缩小一次的问题? 如果有任何不清楚的地方,请告诉我。
解决方法
使用以下技术来实现您的效果 - 像这样覆盖集合视图委托中的 didHighlightItemAt
和 didUnhighlightItemAt
:
func collectionView(_ collectionView: UICollectionView,didHighlightItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
UIView.animate(withDuration: 0.2) {
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.2) {
cell?.transform = .identity
}
}
,
我解决了这个问题:当它缩小或返回时,我必须将 duration
设置为不同的,这样我才能获得平滑的过渡:
func shrink(down: Bool) {
if down {
UIView.animate(withDuration: 0.3) {
self.theView.transform = CGAffineTransform(scaleX: 0.95,y: 0.95)
}
} else {
UIView.animate(withDuration: 0.5) {
self.theView.transform = .identity
}
}
}
override var isHighlighted: Bool {
didSet {
shrink(down: isHighlighted)
}
}
这是平滑过渡后的样子:
如果你想测试结果,这里是我的应用程序的链接:
https://apps.apple.com/de/app/wishlists-einfach-w%C3%BCnschen/id1503912334
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。