How do you set the duration for UICollectionView Animations?

后端 未结 9 2062
旧巷少年郎
旧巷少年郎 2020-12-12 17:07

I have a custom flow layout which is adjusting the attributes for cells when they are being inserted and deleted from the CollectionView with the following two functions, bu

相关标签:
9条回答
  • 2020-12-12 17:39

    After trying [CATransaction setAnimationDuration:] and [UIView setAnimationDuration:] in every possible phase of the layout process without success, I figured out a somewhat hacky way to change the duration of cell animations created by UICollectionView that doesn't rely on private API's.

    You can use CALayer's speed property to change the relative media timing of animations performed on a given layer. For this to work with UICollectionView, you can change layer.speed to something less than 1 on the cell's layer. Obviously it's not great to have the cell's layer ALWAYS have a non-unity animation speed, so one option is to dispatch an NSNotification when preparing for cell animations, to which your cells subscribe, that will change the layer speed, and then change it back at an appropriate time after the animations are finished.

    I don't recommend using this approach as a long-term solution as it's pretty roundabout, but it does work. Hopefully Apple will expose more options for UICollectionView animations in the future.

    0 讨论(0)
  • 2020-12-12 17:41

    change CALayer's speed

    @implementation Cell
    - (id)initWithFrame:(CGRect)frame
    {
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.speed =0.2;//default speed  is 1
    }
    return self;
    }
    
    0 讨论(0)
  • 2020-12-12 17:51

    Without subclassing:

    [UIView animateWithDuration:2.0 animations:^{
      [self.collection reloadSections:indexSet];
    }];
    
    0 讨论(0)
提交回复
热议问题