Skip to content

Commit

Permalink
Refactor GIF works
Browse files Browse the repository at this point in the history
  • Loading branch information
leverdeterre committed Oct 1, 2014
1 parent 0dedf68 commit 9ea9503
Show file tree
Hide file tree
Showing 14 changed files with 215 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
C2323BDD19DC94B000C62D22 /* JMAnimatedGifImageView.m in Sources */ = {isa = PBXBuildFile; fileRef = C2323BDC19DC94B000C62D22 /* JMAnimatedGifImageView.m */; };
C2323BDE19DC94B000C62D22 /* JMAnimatedGifImageView.m in Sources */ = {isa = PBXBuildFile; fileRef = C2323BDC19DC94B000C62D22 /* JMAnimatedGifImageView.m */; };
C24A92DC199F4C0800CFDBE0 /* 0_verge_super_wide.jpg in Resources */ = {isa = PBXBuildFile; fileRef = C24A92D1199F4C0800CFDBE0 /* 0_verge_super_wide.jpg */; };
C24A92DD199F4C0800CFDBE0 /* 10_verge_super_wide.jpg in Resources */ = {isa = PBXBuildFile; fileRef = C24A92D2199F4C0800CFDBE0 /* 10_verge_super_wide.jpg */; };
C24A92DE199F4C0800CFDBE0 /* 1_verge_super_wide.jpg in Resources */ = {isa = PBXBuildFile; fileRef = C24A92D3199F4C0800CFDBE0 /* 1_verge_super_wide.jpg */; };
Expand Down Expand Up @@ -125,8 +127,6 @@
C2B753F9199E88A9004BB1C3 /* UIImage+JM.m in Sources */ = {isa = PBXBuildFile; fileRef = C2B753F6199E88A9004BB1C3 /* UIImage+JM.m */; };
C2B753FC199E8AFE004BB1C3 /* JMTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C2B753FB199E8AFE004BB1C3 /* JMTableViewController.m */; };
C2B753FF199E8B81004BB1C3 /* JMTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = C2B753FE199E8B81004BB1C3 /* JMTableViewCell.m */; };
C2D0974319D6001000AA1980 /* JMAnimatedImageView+JMGif.m in Sources */ = {isa = PBXBuildFile; fileRef = C2D0974219D6001000AA1980 /* JMAnimatedImageView+JMGif.m */; };
C2D0974419D6001000AA1980 /* JMAnimatedImageView+JMGif.m in Sources */ = {isa = PBXBuildFile; fileRef = C2D0974219D6001000AA1980 /* JMAnimatedImageView+JMGif.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -140,6 +140,8 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
C2323BDB19DC94B000C62D22 /* JMAnimatedGifImageView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JMAnimatedGifImageView.h; sourceTree = "<group>"; };
C2323BDC19DC94B000C62D22 /* JMAnimatedGifImageView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JMAnimatedGifImageView.m; sourceTree = "<group>"; };
C24A92D1199F4C0800CFDBE0 /* 0_verge_super_wide.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = 0_verge_super_wide.jpg; sourceTree = "<group>"; };
C24A92D2199F4C0800CFDBE0 /* 10_verge_super_wide.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = 10_verge_super_wide.jpg; sourceTree = "<group>"; };
C24A92D3199F4C0800CFDBE0 /* 1_verge_super_wide.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = 1_verge_super_wide.jpg; sourceTree = "<group>"; };
Expand Down Expand Up @@ -281,8 +283,6 @@
C2B753FB199E8AFE004BB1C3 /* JMTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JMTableViewController.m; sourceTree = "<group>"; };
C2B753FD199E8B81004BB1C3 /* JMTableViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JMTableViewCell.h; sourceTree = "<group>"; };
C2B753FE199E8B81004BB1C3 /* JMTableViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JMTableViewCell.m; sourceTree = "<group>"; };
C2D0974119D6001000AA1980 /* JMAnimatedImageView+JMGif.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "JMAnimatedImageView+JMGif.h"; sourceTree = "<group>"; };
C2D0974219D6001000AA1980 /* JMAnimatedImageView+JMGif.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "JMAnimatedImageView+JMGif.m"; sourceTree = "<group>"; };
C2EAE6E519C0241700B46A27 /* JMAnimatedLog.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = JMAnimatedLog.h; sourceTree = "<group>"; };
C2F4DB6B19BE5C4700F04261 /* JMAmimatedImageViewMacro.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = JMAmimatedImageViewMacro.h; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -535,8 +535,8 @@
C2F4DB6B19BE5C4700F04261 /* JMAmimatedImageViewMacro.h */,
C2B753EF199E88A9004BB1C3 /* JMAnimatedImageView.h */,
C2B753F0199E88A9004BB1C3 /* JMAnimatedImageView.m */,
C2D0974119D6001000AA1980 /* JMAnimatedImageView+JMGif.h */,
C2D0974219D6001000AA1980 /* JMAnimatedImageView+JMGif.m */,
C2323BDB19DC94B000C62D22 /* JMAnimatedGifImageView.h */,
C2323BDC19DC94B000C62D22 /* JMAnimatedGifImageView.m */,
C2B03DFA19A52D000032B240 /* JMAnimatedImageView+Image.h */,
C2B03DFB19A52D000032B240 /* JMAnimatedImageView+Image.m */,
C2B753F1199E88A9004BB1C3 /* JMAnimationOperation.h */,
Expand Down Expand Up @@ -745,13 +745,13 @@
C2B75333199E8802004BB1C3 /* main.m in Sources */,
C2B7533D199E8802004BB1C3 /* JMViewController.m in Sources */,
C2B03E0019A554E30032B240 /* JMFLViewController.m in Sources */,
C2D0974319D6001000AA1980 /* JMAnimatedImageView+JMGif.m in Sources */,
C2954D1719A72C7600494F41 /* AppInformationsManager.m in Sources */,
C2B03DFC19A52D000032B240 /* JMAnimatedImageView+Image.m in Sources */,
C2B753F8199E88A9004BB1C3 /* JMAnimationOperation.m in Sources */,
C2B753F9199E88A9004BB1C3 /* UIImage+JM.m in Sources */,
C2B753FC199E8AFE004BB1C3 /* JMTableViewController.m in Sources */,
C2954D1C19A72C7600494F41 /* UIDevice+iAppInfos.m in Sources */,
C2323BDD19DC94B000C62D22 /* JMAnimatedGifImageView.m in Sources */,
C2954D1B19A72C7600494F41 /* UIApplication+iAppInfos.m in Sources */,
C2B03DF919A474190032B240 /* JMGif.m in Sources */,
C2954D1919A72C7600494F41 /* JMOMobileProvisionning.m in Sources */,
Expand All @@ -765,8 +765,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
C2D0974419D6001000AA1980 /* JMAnimatedImageView+JMGif.m in Sources */,
C2B75352199E8802004BB1C3 /* JMAnimatedImageViewTests.m in Sources */,
C2323BDE19DC94B000C62D22 /* JMAnimatedGifImageView.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
//
// JMAnimatedImageView+JMGif.h
// JMAnimatedGifImageView.h
// JMAnimatedImageView
//
// Created by jerome morissard on 26/09/14.
// Created by jerome morissard on 01/10/14.
// Copyright (c) 2014 jerome morissard. All rights reserved.
//

#import "JMAnimatedImageView.h"
#import "JMGif.h"

@interface JMAnimatedImageView (JMGif)
@interface JMAnimatedGifImageView : JMAnimatedImageView

//Specific to GIF
@property (strong, readonly, nonatomic) JMGif *gifObject;
Expand All @@ -35,5 +35,4 @@
*/
- (void)reloadAnimationImagesFromGifNamed:(NSString *)gitName;


@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
//
// JMAnimatedGifImageView.m
// JMAnimatedImageView
//
// Created by jerome morissard on 01/10/14.
// Copyright (c) 2014 jerome morissard. All rights reserved.
//

#import "JMAnimatedGifImageView.h"
#import "JMAnimatedLog.h"
#import "JMAnimationOperation.h"

@implementation JMAnimatedGifImageView

- (BOOL)isAGifImageView
{
if (self.gifObject) {
return YES;
}
return NO;
}

- (void)reloadAnimationImagesFromGifData:(NSData *)data
{
_gifObject = [[JMGif alloc] initWithData:data];
self.animationDuration = JMDefaultGifDuration;
[self setCurrentIndex:0];
}

- (void)reloadAnimationImagesFromGifNamed:(NSString *)gitName
{
_gifObject = [JMGif gifNamed:gitName];
self.animationDuration = JMDefaultGifDuration;
[self setCurrentIndex:0];
[self updateGestures];
}

- (void)moveCurrentCardImageFromIndex:(NSInteger)fromIndex
shift:(NSInteger)shift
withDuration:(NSTimeInterval)duration
animationOption:(UIImageViewAnimationOption)option
withCompletionBlock:(JMCompletionFinishBlock)finishBlock
{
dispatch_async(self.animationManagementQueue, ^{

NSTimeInterval unitDuration;
NSInteger shiftUnit = shift / abs((int)shift); // 1 ou -1

if (duration == JMDefaultGifDuration) {
unitDuration = duration;

} else {
if (option == UIImageViewAnimationOptionLinear) {
unitDuration = duration / abs((int)shift);
} else {
unitDuration = duration / abs((int)(shift * shift));
}
}

JMLog(@"%s fromIndex:%d shift:%d duration:%lf",__FUNCTION__,(int)fromIndex,(int)shift,duration);

//[self cancelAnimations];
[self.animationQueue cancelAllOperations];
[self.animationQueue waitUntilAllOperationsAreFinished];

for (int i = 0; i < (int)abs((int)shift) ; i++) {

NSInteger index = [self realIndexForComputedIndex:fromIndex+i*shiftUnit];
JMGifItem *item = [[self.gifObject items] objectAtIndex:index];

JMAnimationOperation *operation = [JMAnimationOperation animationOperationWithDuration:item.delayDuration
completion:^(BOOL finished)
{

if (self.animationType == JMAnimatedImageViewAnimationTypeAutomaticLinearWithoutTransition) {
if ([self operationQueueIsFinished] == YES) {
if (finishBlock) {
finishBlock(YES);
}

if (self.animationRepeatCount == 0 && self.animationState == UIImageViewAnimationStateInPgrogress) {
[self continueAnimating];
}
}
}
}];

operation.animatedImageView = self;
operation.imageIndex = index;
[self.animationQueue addOperation:operation];
}
});
}

- (void)startAnimating
{
self.animationState = UIImageViewAnimationStateInPgrogress;

if ([self checkLifeCycleSanity] == NO) {
return;
}

if (self.animationType == JMAnimatedImageViewAnimationTypeAutomaticLinearWithoutTransition) {
[self moveCurrentCardImageFromIndex:self.currentIndex
shift:self.gifObject.items.count
withDuration:self.animationDuration
animationOption:UIImageViewAnimationOptionLinear
withCompletionBlock:NULL];

} else if (self.animationType == JMAnimatedImageViewAnimationTypeAutomaticLinear) {
[self changeImageToIndex:(self.currentIndex + 1) withTimeInterval:self.animationDuration repeat:YES];
}
}

- (void)continueAnimating
{
self.animationState = UIImageViewAnimationStateInPgrogress;

dispatch_async(dispatch_get_main_queue(), ^{
if ([self checkLifeCycleSanity] == NO) {
return;
}

if ([self operationQueueIsFinished] == NO) {
return;
}

[self moveCurrentCardImageFromIndex:self.currentIndex
shift:self.gifObject.items.count
withDuration:self.animationDuration
animationOption:UIImageViewAnimationOptionLinear
withCompletionBlock:NULL];
});
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@

#import "JMAnimatedImageView+Image.h"
#import "UIImage+JM.h"
#import "JMAnimatedImageView+JMGif.h"
#import "JMAnimatedGifImageView.h"

@implementation JMAnimatedImageView (Image)

- (UIImage *)imageAtIndex:(NSInteger)index
{
if ([self isAGifImageView]) {
return [self.gifObject imageAtIndex:index];
if ([self isKindOfClass:[JMAnimatedGifImageView class]]) {
JMAnimatedGifImageView *gifView = (JMAnimatedGifImageView *)self;
return [gifView.gifObject imageAtIndex:index];

} else if ([self.animationDatasource respondsToSelector:@selector(imageAtIndex:forAnimatedImageView:)]) {
return [self.animationDatasource imageAtIndex:index forAnimatedImageView:self];
Expand All @@ -31,8 +32,10 @@ - (UIImage *)imageAtIndex:(NSInteger)index

- (NSUInteger)numberOfImages
{
if ([self isAGifImageView]) {
return self.gifObject.items.count;
if ([self isKindOfClass:[JMAnimatedGifImageView class]]) {
JMAnimatedGifImageView *gifView = (JMAnimatedGifImageView *)self;
return gifView.gifObject.items.count;

} else {
if ([self.animationDatasource respondsToSelector:@selector(numberOfImagesForAnimatedImageView:)]) {
return [self.animationDatasource numberOfImagesForAnimatedImageView:self];
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ typedef NS_ENUM(NSUInteger, JMAnimatedImageViewOrder) {
JMAnimatedImageViewOrderReverse = -1
};

typedef NS_ENUM(NSUInteger, UIImageViewAnimationOption) {
UIImageViewAnimationOptionLinear = 0,
UIImageViewAnimationOptionCurveEaseInOut
};

typedef NS_ENUM(NSUInteger, UIImageViewAnimationState) {
UIImageViewAnimationStateStopped = 0,
UIImageViewAnimationStateInPgrogress
};

typedef void (^JMCompletionFinishBlock)(BOOL resul);

@interface JMAnimatedImageView : UIImageView
Expand All @@ -41,7 +51,10 @@ typedef void (^JMCompletionFinishBlock)(BOOL resul);
@property (assign, nonatomic) JMAnimatedImageViewMemoryOption memoryManagementOption;
@property (assign, nonatomic) JMAnimatedImageViewOrder imageOrder;
@property (assign, nonatomic) BOOL interactiveAnimation;
@property (nonatomic, assign) NSInteger currentIndex;
@property (assign, nonatomic) NSInteger currentIndex;
@property (assign, nonatomic) UIImageViewAnimationState animationState;
@property (strong, nonatomic, readonly) dispatch_queue_t animationManagementQueue;
@property (strong, nonatomic, readonly) NSOperationQueue *animationQueue;

/**
* reloadAnimationImages, This method will call animationDatasource
Expand Down Expand Up @@ -82,5 +95,9 @@ typedef void (^JMCompletionFinishBlock)(BOOL resul);
- (void)animateToIndex:(NSInteger)index withDuration:(NSTimeInterval)duration withCompletionBlock:(JMCompletionFinishBlock)finishBlock;

- (void)updateGestures;
- (void)changeImageToIndex:(NSInteger)index withTimeInterval:(NSTimeInterval)duration repeat:(BOOL)repeat;
- (BOOL)operationQueueIsFinished;
- (BOOL)checkLifeCycleSanity;
- (NSInteger)realIndexForComputedIndex:(NSInteger)index;

@end
Loading

0 comments on commit 9ea9503

Please sign in to comment.