Skip to content

Commit

Permalink
little refactor to support GIF
Browse files Browse the repository at this point in the history
  • Loading branch information
leverdeterre committed Sep 29, 2014
1 parent 2ee544d commit 0dedf68
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@
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 Down Expand Up @@ -279,6 +281,8 @@
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 @@ -531,6 +535,8 @@
C2F4DB6B19BE5C4700F04261 /* JMAmimatedImageViewMacro.h */,
C2B753EF199E88A9004BB1C3 /* JMAnimatedImageView.h */,
C2B753F0199E88A9004BB1C3 /* JMAnimatedImageView.m */,
C2D0974119D6001000AA1980 /* JMAnimatedImageView+JMGif.h */,
C2D0974219D6001000AA1980 /* JMAnimatedImageView+JMGif.m */,
C2B03DFA19A52D000032B240 /* JMAnimatedImageView+Image.h */,
C2B03DFB19A52D000032B240 /* JMAnimatedImageView+Image.m */,
C2B753F1199E88A9004BB1C3 /* JMAnimationOperation.h */,
Expand Down Expand Up @@ -739,6 +745,7 @@
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 */,
Expand All @@ -758,6 +765,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
C2D0974419D6001000AA1980 /* JMAnimatedImageView+JMGif.m in Sources */,
C2B75352199E8802004BB1C3 /* JMAnimatedImageViewTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,16 @@
<Bucket
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.ExceptionBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
scope = "0"
stopOnStyle = "0">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<rect key="frame" x="0.0" y="0.0" width="1024" height="768"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" image="zoom_1920_0.jpg" translatesAutoresizingMaskIntoConstraints="NO" id="2BC-tM-Ukq" customClass="JMAnimatedImageView">
<imageView userInteractionEnabled="NO" contentMode="scaleAspectFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" image="zoom_1920_0.jpg" translatesAutoresizingMaskIntoConstraints="NO" id="2BC-tM-Ukq" customClass="JMAnimatedImageView">
<rect key="frame" x="10" y="149" width="1004" height="496"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
</imageView>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

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

@implementation JMAnimatedImageView (Image)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// JMAnimatedImageView+JMGif.h
// JMAnimatedImageView
//
// Created by jerome morissard on 26/09/14.
// Copyright (c) 2014 jerome morissard. All rights reserved.
//

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

@interface JMAnimatedImageView (JMGif)

//Specific to GIF
@property (strong, readonly, nonatomic) JMGif *gifObject;

/**
* isAGifImageView
*
* @return BOOL
*/
- (BOOL)isAGifImageView;

/**
* reloadAnimationImagesFromGifData:, This method reload a GIF image from a GIF NSData
*
* @param data NSData data
*/
- (void)reloadAnimationImagesFromGifData:(NSData *)data;

/**
* reloadAnimationImagesFromGifNamed:, This method reload a GIF image from a GIF named
*
* @param gitName NSString gitName
*/
- (void)reloadAnimationImagesFromGifNamed:(NSString *)gitName;


@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// JMAnimatedImageView+JMGif.m
// JMAnimatedImageView
//
// Created by jerome morissard on 26/09/14.
// Copyright (c) 2014 jerome morissard. All rights reserved.
//

#import "JMAnimatedImageView+JMGif.h"
#import <objc/runtime.h>

//JMGif *gifObject

@implementation JMAnimatedImageView (JMGif)

- (JMGif *)gifObject
{
return objc_getAssociatedObject(self, _cmd);
}

- (void)setGifObject:(JMGif *)gifObject
{
objc_setAssociatedObject(self, @selector(gifObject), gifObject, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

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

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

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

@end
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#import <UIKit/UIKit.h>
#import "JMOImageViewAnimationDelegate.h"
#import "JMOImageViewAnimationDatasource.h"
#import "JMGif.h"

#define JMDefaultGifDuration -1

Expand Down Expand Up @@ -42,6 +41,7 @@ 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;

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

//Specific to GIF
@property (strong, readonly, nonatomic) JMGif *gifObject;

/**
* isAGifImageView
*
* @return BOOL
*/
- (BOOL)isAGifImageView;

/**
* reloadAnimationImagesFromGifData:, This method reload a GIF image from a GIF NSData
*
* @param data NSData data
*/
- (void)reloadAnimationImagesFromGifData:(NSData *)data;

/**
* reloadAnimationImagesFromGifNamed:, This method reload a GIF image from a GIF named
*
* @param gitName NSString gitName
*/
- (void)reloadAnimationImagesFromGifNamed:(NSString *)gitName;
- (void)updateGestures;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "UIImage+JM.h"
#import "JMAnimatedImageView+Image.h"
#import "JMAnimatedLog.h"
#import "JMAnimatedImageView+JMGif.h"

typedef NS_ENUM(NSUInteger, UIImageViewAnimationOption) {
UIImageViewAnimationOptionLinear = 0,
Expand All @@ -23,7 +24,6 @@ typedef NS_ENUM(NSUInteger, UIImageViewAnimationState) {
};

@interface JMAnimatedImageView()
@property (nonatomic, assign) NSInteger currentIndex;
@property (nonatomic, assign) NSInteger operationInQueue;
@property (nonatomic, strong) NSOperationQueue *animationQueue;
@property (nonatomic, strong) UIPanGestureRecognizer *panGesture;
Expand Down Expand Up @@ -75,27 +75,12 @@ - (void)setup
- (void)reloadAnimationImages
{
if ([self.animationDatasource respondsToSelector:@selector(firstIndexForAnimatedImageView:)]) {
[self setCurrentCardImageAtindex:[self.animationDatasource firstIndexForAnimatedImageView:self]];
[self setCurrentIndex:[self.animationDatasource firstIndexForAnimatedImageView:self]];
} else {
[self setCurrentCardImageAtindex:0];
[self setCurrentIndex:0];
}
}

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

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

- (BOOL)checkLifeCycleSanity
{
if (self.superview) {
Expand All @@ -107,10 +92,17 @@ - (BOOL)checkLifeCycleSanity

- (void)setCurrentIndex:(NSInteger)currentIndex
{
_currentIndex = currentIndex;
NSInteger realIndex = [self realIndexForComputedIndex:currentIndex];

_currentIndex = realIndex;
if ([self.animationDelegate respondsToSelector:@selector(imageView:didChangeCurrentindex:)]) {
[self.animationDelegate imageView:self didChangeCurrentindex:_currentIndex];
}

self.image = [self imageAtIndex:_currentIndex];
if (self.animationType == JMAnimatedImageViewAnimationTypeManualSwipe) {
[self updatePageControl];
}
}

- (void)setupTempImageViewAtOriginiX:(CGFloat)x
Expand Down Expand Up @@ -140,14 +132,6 @@ - (void)setupTempImageViewAtOriginiX:(CGFloat)x

#pragma mark - overided setter

- (BOOL)isAGifImageView
{
if (_gifObject) {
return YES;
}
return NO;
}

- (void)setInteractiveAnimation:(BOOL)interactiveAnimation
{
_interactiveAnimation = interactiveAnimation;
Expand Down Expand Up @@ -269,9 +253,9 @@ - (void)imageViewTouchedWithFollowingPanGesture:(UIPanGestureRecognizer *)gestur
self.userInteractionEnabled = YES;
if (finishSwipeEvent) {
if (velocity.x > 0) {
[self setCurrentCardImageAtindex:[self realIndexForComputedIndex:self.currentIndex + 1]];
[self setCurrentIndex:[self realIndexForComputedIndex:self.currentIndex + 1]];
} else {
[self setCurrentCardImageAtindex:[self realIndexForComputedIndex:self.currentIndex -1 ]];
[self setCurrentIndex:[self realIndexForComputedIndex:self.currentIndex -1 ]];
}
}
}];
Expand Down Expand Up @@ -304,9 +288,9 @@ - (void)imageViewTouchedWithPanGesture:(UIPanGestureRecognizer *)gestureRecogniz
NSInteger shift = abs(velocity.x) / (16 * [UIScreen mainScreen].scale * pointUnity);

if(velocity.x > 0) {
[self setCurrentCardImageAtindex:index+(_imageOrder) * shift];
[self setCurrentIndex:index+(_imageOrder) * shift];
} else {
[self setCurrentCardImageAtindex:index-(_imageOrder) * shift];
[self setCurrentIndex:index-(_imageOrder) * shift];
}

/*
Expand Down Expand Up @@ -342,28 +326,6 @@ - (void)imageViewTouched:(UIGestureRecognizer *)gestureRecognizer

#pragma mark - Load images

- (void)setCurrentCardImageAtindex:(NSInteger)index
{
NSInteger realIndex = [self realIndexForComputedIndex:index];
self.image = [self imageAtIndex:realIndex];
self.currentIndex = realIndex;

if (self.animationType == JMAnimatedImageViewAnimationTypeManualSwipe) {
[self updatePageControl];
}
}

- (void)setCurrentImage:(UIImage *)img forIndex:(NSInteger)index
{
JMLog(@"%s index:%d",__FUNCTION__,(int)index);
NSInteger realIndex = [self realIndexForComputedIndex:index];
self.image = img;
self.currentIndex = realIndex;
if (self.animationType == JMAnimatedImageViewAnimationTypeManualSwipe) {
[self updatePageControl];
}
}

- (NSInteger)realIndexForComputedIndex:(NSInteger)index
{
NSInteger nb = [self numberOfImages];
Expand All @@ -389,6 +351,7 @@ - (void)moveCurrentCardImageFromIndex:(NSInteger)fromIndex
withCompletionBlock:(JMCompletionFinishBlock)finishBlock
{
dispatch_async(self.animationManagementQueue, ^{

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

Expand Down Expand Up @@ -476,7 +439,7 @@ - (void)changeImageToIndex:(NSInteger)index withTimeInterval:(NSTimeInterval)dur

} completion:^(BOOL finished) {
[self.tempSwapedImageView removeFromSuperview];
[self setCurrentCardImageAtindex:[self realIndexForComputedIndex:index]];
[self setCurrentIndex:[self realIndexForComputedIndex:index]];
if (repeat) {
[self changeImageToIndex:(self.currentIndex+1) withTimeInterval:duration repeat:repeat];
}
Expand Down Expand Up @@ -557,7 +520,7 @@ - (void)startAnimating
if (self.animationType == JMAnimatedImageViewAnimationTypeAutomaticLinearWithoutTransition) {
if ([self isAGifImageView]) {
[self moveCurrentCardImageFromIndex:self.currentIndex
shift:_gifObject.items.count
shift:self.gifObject.items.count
withDuration:self.animationDuration
animationOption:UIImageViewAnimationOptionLinear
withCompletionBlock:NULL];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "JMFLViewController.h"
#import "JMAnimatedImageView.h"
#import "JMAnimatedImageView+JMGif.h"

@interface JMFLViewController ()
@property (weak, nonatomic) IBOutlet JMAnimatedImageView *carImageView1;
Expand Down
Loading

0 comments on commit 0dedf68

Please sign in to comment.