Skip to content

Instantly share code, notes, and snippets.

@eoghain
Created March 5, 2014 00:16
Show Gist options
  • Save eoghain/9358631 to your computer and use it in GitHub Desktop.
Save eoghain/9358631 to your computer and use it in GitHub Desktop.
When adding/removing/reloading cells in a UICollectionView using the FlowLayout the cells fade in/out. When doing things like updating a progress bar you get a lot of annoying flashing because of this, this category will prevent that. As an added benefit rotation animations will now show the cells moving into position.
//
// UICollectionViewFlowLayout+NoFade.m
//
// Created by Rob Booth on 3/4/14.
#import "UICollectionViewFlowLayout+NoFade.h"
#import <objc/runtime.h>
@implementation UICollectionViewFlowLayout (NoFade)
+ (void) load
{
Method original, swizzled;
original = class_getInstanceMethod(self, @selector(initialLayoutAttributesForAppearingItemAtIndexPath:));
swizzled = class_getInstanceMethod(self, @selector(noFadeInitialLayoutAttributesForAppearingItemAtIndexPath:));
method_exchangeImplementations(original, swizzled);
original = class_getInstanceMethod(self, @selector(finalLayoutAttributesForDisappearingItemAtIndexPath:));
swizzled = class_getInstanceMethod(self, @selector(noFadeFinalLayoutAttributesForDisappearingItemAtIndexPath:));
method_exchangeImplementations(original, swizzled);
}
- (UICollectionViewLayoutAttributes *)noFadeInitialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
{
// Not recursive, due to method exhange in load above
// This is because we can't call super in a catagory
UICollectionViewLayoutAttributes * attributes = [self noFadeInitialLayoutAttributesForAppearingItemAtIndexPath:itemIndexPath];
attributes.alpha = 1.0;
return attributes;
}
- (UICollectionViewLayoutAttributes *)noFadeFinalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
{
// Not recursive, due to method exhange in load above
// This is because we can't call super in a catagory
UICollectionViewLayoutAttributes * attributes = [self noFadeFinalLayoutAttributesForDisappearingItemAtIndexPath:itemIndexPath];
attributes.alpha = 1.0;
return attributes;
}
@end
@zolomatok
Copy link

zolomatok commented Sep 17, 2018

Thank you, @tooolbox!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment