【问题标题】:Custom UICollectionViewFlowLayout animation自定义 UICollectionViewFlowLayout 动画
【发布时间】:2015-08-03 21:26:42
【问题描述】:

我有一个自定义的 UICollectionViewFlowLayout 动画,该动画将视图从右侧交错插入,从左侧交错删除。它通过在 UICollectionViewLayoutAttributes 上设置 CABasicAnimation 并将其应用于单元格层来实现。

CollectionViewAnimations Project on GitHub

默认 alpha 为 0,它会淡出我的单元格并提前结束我的自定义动画。如果我将 alpha 更改为 1,那么我根本看不到我的动画。我将它设置为 0.5,我得到了两者的一点......这很奇怪。你必须运行我的项目才能明白我的意思。

AnimatingFlowLayout.swift

由于某种原因,我似乎无法完全删除 finalLayoutAttributesForDisappearingItemAtIndexPath 中属性的默认 alpha。

有人有什么想法吗?

【问题讨论】:

    标签: ios uicollectionviewlayout


    【解决方案1】:

    您正在使用performBatchUpdates(_:completion:),它已经为您在finalLayoutAttributesForDisappearingItemAtIndexPath(_:) 中设置的更改设置了动画,因此如果您添加CABasicAnimation,您将向已经发生的动画添加动画。如果您从CellLayoutAttributes 中删除动画并设置UICollectionViewLayoutAttributestransform3D,它会做您想做的事情(除了动画beginTimefillMode)。这段代码很适合我:

    override func finalLayoutAttributesForDisappearingItemAtIndexPath(itemIndexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
        let attributes: CellLayoutAttributes = super.finalLayoutAttributesForDisappearingItemAtIndexPath(itemIndexPath) as! CellLayoutAttributes
        // Default is 0, if I set it to 1.0 you don't see anything happen..'
        attributes.alpha = 1
        let endX = -CGRectGetWidth(self.collectionView!.frame)
        var endTransform: CATransform3D = CATransform3DMakeTranslation(endX, 0, 0)
        attributes.transform3D = endTransform
    
        return attributes
    } 
    

    【讨论】:

    • 要获得动画的beginTime 效果,您可以像他们建议的here 那样一个接一个地添加您的单元格。 UICollectionView 也使用默认持续时间来为单元格的外观设置动画。并且看起来没有办法以适当/非 hacky 的方式对其进行更改。
    • “除了动画 beginTime 和 fillMode”——这就是这个动画的重点……交错效果。
    • 点是你正在为已经进行的动画添加动画
    【解决方案2】:

    这对我有用,类似的问题:

    import UIKit
    
    class NoFadeFlowLayout: UICollectionViewFlowLayout {
    
        override func initialLayoutAttributesForAppearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
            let attrs = super.initialLayoutAttributesForAppearingItem(at: itemIndexPath)
            attrs?.alpha = 1.0
            return attrs
        }
    
        override func finalLayoutAttributesForDisappearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
            let attrs = super.finalLayoutAttributesForDisappearingItem(at: itemIndexPath)
            attrs?.alpha = 1.0
            return attrs
        }
    
    }
    

    你说你不能让默认的 alpha 在这个方法中消失,但是当我在 tvOS 11 上尝试它时它起作用了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-10
      • 2016-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-02
      • 2012-02-28
      相关资源
      最近更新 更多