Skip to content

Commit

Permalink
Fix reuse cell animation and now happens when the user clic the cell …
Browse files Browse the repository at this point in the history
…and not when the user scroll and the reusable cell appears
  • Loading branch information
Roberto Halgravez committed Jun 22, 2017
1 parent cff649a commit 0f5c483
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
13 changes: 12 additions & 1 deletion CollectionSample/CollectionSample/AGTColorfulViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "AGTColorfulViewController.h"
#import "AGTColors.h"
#import "AGTRandomColorCell.h"
#import "UIColor+Colorful.h"

@interface AGTColorfulViewController ()

Expand Down Expand Up @@ -58,7 +59,7 @@ -(instancetype)initWithModel:(AGTColors *)model layout:(UICollectionViewLayout *
if (self = [super initWithCollectionViewLayout:layout]) {
_model = model;
self.title = @"United colors of Agbo";
self.maxRandomColorsToDisplay = 3;
self.maxRandomColorsToDisplay = 104;
}

return self;
Expand Down Expand Up @@ -170,4 +171,14 @@ -(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView v
return subView;
}

#pragma mark - Delegate
-(void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

if (indexPath.section == [AGTColorfulViewController radomColorSection]) {
AGTRandomColorCell *cell = (AGTRandomColorCell*)[collectionView cellForItemAtIndexPath:indexPath];
cell.color = [cell.color complementaryColor];
}

}

@end
1 change: 1 addition & 0 deletions CollectionSample/CollectionSample/AGTRandomColorCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
@property (weak, nonatomic) IBOutlet UILabel *hexView;

@property (nonatomic, strong) UIColor *color;
@property (nonatomic) BOOL shouldAnimateChangeOfColor;

@end
18 changes: 17 additions & 1 deletion CollectionSample/CollectionSample/AGTRandomColorCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@
@implementation AGTRandomColorCell

-(void)awakeFromNib {
self.shouldAnimateChangeOfColor = NO;
[self setupKVO];
}

-(void) dealloc {
[self tearDownKVO];
}

#pragma mark - View lifecycle
-(void)prepareForReuse {
[super prepareForReuse];

self.shouldAnimateChangeOfColor = NO;
self.color = [UIColor x11WhiteColor];
}

#pragma mark - KVO
-(void)setupKVO {
[self addObserver:self forKeyPath:@"color" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];
Expand All @@ -34,10 +43,17 @@ -(void)observeValueForKeyPath:(NSString *)keyPath
context:(void *)context {
self.hexView.text = [self.color hexString];

[UIView animateWithDuration:0.6 animations:^{
float duration = 0.0f;
if (self.shouldAnimateChangeOfColor) {
duration = 0.6f;
}

[UIView animateWithDuration:duration animations:^{
self.backgroundColor = self.color;
self.hexView.textColor = [self.color contrastingTextColor];
}];

self.shouldAnimateChangeOfColor = YES;
}

@end

0 comments on commit 0f5c483

Please sign in to comment.