Created
March 5, 2014 00:16
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The modern way to do this would be subclassing the flow layout, like so: