Has*_*h88 9 animation ios uicollectionview uicollectionviewcell swift
animated这些方法的参数有什么作用UICollectionView:
selectItem(at indexPath: IndexPath?, animated: Bool, scrollPosition: UICollectionViewScrollPosition)`
Run Code Online (Sandbox Code Playgroud)
deselectItem(at indexPath: IndexPath, animated: Bool)
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用该UICollectionViewLayout对象来为更改设置动画。我也知道我可以使用didSelect和 的didDeselect方法UICollectionViewDelegate来获取选定的单元格并应用动画。但是我找不到有关上述animated参数如何影响动画的任何信息。它会以任何方式影响布局动画吗?我的目标是创建一个UICollectionView子类,并允许消费者自定义在内部调用上述两种方法时是否应用动画。但我不知道这些方法控制什么动画。
该animated在UICollectionView的selectItem(at:animated:scrollPosition:)确定是否项待选择的,如果不是在视图或所需的位置早已,应以动画方式或不滚动以。
如果它在视野中,那么这个animated属性并没有真正做任何事情,afaik。
相同的animatedin deselectItem(at:animated:)。它什么都不做,就在那里。
我看到的唯一影响布局引擎的是,如果collectionView滚动并且你有动画,didSelectItemAt那么它会使这些动画无效。您将不得不延迟单元格中发生的动画(请参阅此答案中的最后一个示例)
正如您已经知道的,但对于其他人,如果您想为单元格选择事件设置动画,那么您必须在collectionView(_:didSelectItemAt:)委托中自己完成。
例子:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
//Briefly fade the cell on selection
UIView.animate(withDuration: 0.5,
animations: {
//Fade-out
cell?.alpha = 0.5
}) { (completed) in
UIView.animate(withDuration: 0.5,
animations: {
//Fade-out
cell?.alpha = 1
})
}
}
Run Code Online (Sandbox Code Playgroud)
如果用户点击单元格,则上述内容很好,但如果您以编程方式调用selectItem(at:animated:scrollPosition:),则不会触发上述collectionView(_:didSelectItemAt:)委托,您需要显式调用它来运行您的选择动画。
示例(上一个的附加组件):
func doSelect(for aCollectionView: UICollectionView,
at indexPath: IndexPath) {
aCollectionView.selectItem(at: indexPath,
animated: true,
scrollPosition: .centeredVertically)
//DispatchQueue after sometime because scroll animation renders
//the animation block in `collectionView(_:didSelectItemAt:)` ineffective
DispatchQueue.main.asyncAfter(deadline: .now() + 0.27) { [weak self] in
self?.collectionView(aCollectionView,
didSelectItemAt: indexPath)
}
}
Run Code Online (Sandbox Code Playgroud)
我建议覆盖with 动画的isHighlighted属性UICollectionViewCell。这样,如果用户点击一个单元格并停下来思考下一步要做什么,动画状态将被保留。
override var isHighlighted: Bool {
didSet {
toggleIsHighlighted()
}
}
func toggleIsHighlighted() {
UIView.animate(withDuration: 0.1, delay: 0, options: [.curveEaseOut], animations: {
self.alpha = self.isHighlighted ? 0.9 : 1.0
self.transform = self.isHighlighted ?
CGAffineTransform.identity.scaledBy(x: 0.97, y: 0.97) :
CGAffineTransform.identity
})
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6756 次 |
| 最近记录: |