Skip to content

Commit

Permalink
Refactor numberOfImages access for GIF better support
Browse files Browse the repository at this point in the history
  • Loading branch information
leverdeterre committed Aug 20, 2014
1 parent 3ae0531 commit af73a43
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 11 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
@interface JMAnimatedImageView (Image)

- (UIImage *)imageAtIndex:(NSInteger)index;
- (NSUInteger)numberOfImages;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,17 @@ - (UIImage *)imageAtIndex:(NSInteger)index
return nil;
}

- (NSUInteger)numberOfImages
{
if ([self isAGifImageView]) {
return self.gifObject.items.count;
} else {
if ([self.animationDatasource respondsToSelector:@selector(numberOfImagesForAnimatedImageView:)]) {
return [self.animationDatasource numberOfImagesForAnimatedImageView:self];
}
}

return 0;
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ typedef NS_ENUM(NSUInteger, JMAnimatedImageViewMemoryOption) {
};

typedef NS_ENUM(NSUInteger, JMAnimatedImageViewOrder) {
JMAnimatedImageViewOrderNone = 0,
JMAnimatedImageViewOrderNormal = 1,
JMAnimatedImageViewOrderReverse = -1
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ - (void)addGesturesForAnimationType:(JMAnimatedImageViewAnimationType)animationT
floorf(CGRectGetWidth(self.frame) * 0.3),
30.0f);
self.pageControl = [[UIPageControl alloc] initWithFrame:pageControlFrame];
self.pageControl.numberOfPages = [self.animationDatasource numberOfImagesForAnimatedImageView:self];
self.pageControl.numberOfPages = [self numberOfImages];
self.pageControl.currentPage = 0;
[self addSubview:self.pageControl];
}
Expand Down Expand Up @@ -268,10 +268,10 @@ - (void)imageViewTouchedWithPanGesture:(UIPanGestureRecognizer *)gestureRecogniz
NSInteger index = [self currentIndex];

//compute point unity
NSInteger pointUnity = self.frame.size.width / [self.animationDatasource numberOfImagesForAnimatedImageView:self];
NSInteger pointUnity = self.frame.size.width / [self numberOfImages];

//Compute inerty using velocity
NSInteger shift = abs(velocity.x) / (8 * [UIScreen mainScreen].scale * pointUnity);
NSInteger shift = abs(velocity.x) / (4 * [UIScreen mainScreen].scale * pointUnity);

if(velocity.x > 0) {
[self setCurrentCardImageAtindex:index+(_imageOrder) * shift];
Expand Down Expand Up @@ -330,7 +330,7 @@ - (void)setCurrentImage:(UIImage *)img forIndex:(NSInteger)index

- (NSInteger)realIndexForComputedIndex:(NSInteger)index
{
NSInteger nb = [self.animationDatasource numberOfImagesForAnimatedImageView:self];
NSInteger nb = [self numberOfImages];

if (index < 0) {
return nb + index;
Expand Down Expand Up @@ -438,7 +438,7 @@ - (void)changeImageToIndex:(NSInteger)index withTimeInterval:(NSTimeInterval)dur

- (void)animateToIndex:(NSInteger)index withDuration:(NSTimeInterval)duration
{
[self moveCurrentCardImageFromIndex:self.currentIndex shift:([self.animationDatasource numberOfImagesForAnimatedImageView:self] - self.currentIndex) withDuration:duration animationOption:UIImageViewAnimationOptionLinear];
[self moveCurrentCardImageFromIndex:self.currentIndex shift:([self numberOfImages] - self.currentIndex) withDuration:duration animationOption:UIImageViewAnimationOptionLinear];
}

- (void)setCurrentIndex:(NSInteger)index animated:(BOOL)animated
Expand Down Expand Up @@ -474,8 +474,7 @@ - (void)startAnimating
animationOption:UIImageViewAnimationOptionLinear];
} else {
[self moveCurrentCardImageFromIndex:0
shift:[self.animationDatasource
numberOfImagesForAnimatedImageView:self]
shift:[self numberOfImages]
withDuration:self.animationDuration
animationOption:UIImageViewAnimationOptionLinear];
}
Expand Down Expand Up @@ -503,8 +502,7 @@ - (void)continueAnimating
animationOption:UIImageViewAnimationOptionLinear];
} else {
[self moveCurrentCardImageFromIndex:0
shift:[self.animationDatasource
numberOfImagesForAnimatedImageView:self]
shift:[self numberOfImages]
withDuration:self.animationDuration
animationOption:UIImageViewAnimationOptionLinear];
}
Expand Down
15 changes: 14 additions & 1 deletion JMAnimatedImageView/JMAnimatedImageView/JMTableViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ - (void)didReceiveMemoryWarning

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 7;
return 8;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Expand Down Expand Up @@ -68,9 +68,14 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
} else if (indexPath.row == 5) {
cell.jmLabel.text = @"SIMPLE CAROUSEL : using JMAnimatedImageView (Low memory usage)";
cell.jmDetailsLabel.text = @"Swipe left / Right";

} else if (indexPath.row == 6) {
cell.jmLabel.text = @"GIF ANIMATION : using JMAnimatedImageView (Low memory usage)";
cell.jmDetailsLabel.text = @"Swipe left / Right";

} else if (indexPath.row == 7) {
cell.jmLabel.text = @"GIF INTERACTION : using JMAnimatedImageView (Low memory usage)";
cell.jmDetailsLabel.text = @"Swipe left / Right";
}

return cell;
Expand Down Expand Up @@ -113,6 +118,14 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
vc.memoryManagementOption = JMAnimatedImageViewMemoryLoadImageLowMemoryUsage;
vc.useJMImageView = YES;
vc.usingGif = YES;
vc.order = JMAnimatedImageViewOrderNormal;

} else if (indexPath.row == 7) {
vc.animationType = JMAnimatedImageViewAnimationTypeManualRealTime;
vc.memoryManagementOption = JMAnimatedImageViewMemoryLoadImageLowMemoryUsage;
vc.useJMImageView = YES;
vc.usingGif = YES;
vc.order = JMAnimatedImageViewOrderNormal;
}

[self.navigationController pushViewController:vc animated:YES];
Expand Down

0 comments on commit af73a43

Please sign in to comment.