Skip to content

Commit 87c4b01

Browse files
author
Albert M
committed
- readme and improvements
1 parent c7a365e commit 87c4b01

4 files changed

Lines changed: 204 additions & 17 deletions

File tree

Pod/Classes/UIImageView+SoftFrameAnimations.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,15 @@
4646
@property(nonatomic,weak) id<SoftFrameAnimationsDelegate> delegate;
4747

4848
-(void)setIdleAnimation:(NSString *)idleAnimationName numberOfDigits:(NSInteger)digits firstDigit:(NSInteger)firstDigit andExtension:(NSString *)ext startNow:(BOOL)startNow andFPS:(CGFloat)framesPerSecond;
49+
-(void)playIdle;
50+
-(void)resumeIdle;
51+
-(void)pauseIdle;
52+
-(void)removeIdle;
4953

5054
-(void)softFrameAnimateWithImageName:(NSString *)imageName numberOfDigits:(NSInteger)digits firstDigit:(NSInteger)firstDigit andExtension:(NSString *)ext loop:(BOOL)loop loopCount:(NSInteger)loopCount andFPS:(CGFloat)framesPerSecond;
5155
-(void)pauseSoftFrameAnimation;
52-
-(void)playSoftFrameAnimation;
56+
-(void)resumeSoftFrameAnimation;
57+
5358
-(void)setImagePath:(NSString *)image;
5459
-(NSString *)getImageNameDisplayed;
5560

Pod/Classes/UIImageView+SoftFrameAnimations.m

Lines changed: 83 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ - (id)initWithWeakObject:(id)weakObject
2222

2323
@implementation UIImageView (SoftFrameAnimations)
2424

25-
#pragma mark - Soft animations
25+
#pragma mark - SoftFrameAnimations Idle
2626

2727
-(void)setIdleAnimation:(NSString *)idleAnimationName numberOfDigits:(NSInteger)digits firstDigit:(NSInteger)firstDigit andExtension:(NSString *)ext startNow:(BOOL)startNow andFPS:(CGFloat)framesPerSecond{
2828
self.idleimagename = idleAnimationName;
@@ -35,26 +35,50 @@ -(void)setIdleAnimation:(NSString *)idleAnimationName numberOfDigits:(NSInteger
3535
}
3636
}
3737

38-
+(UIImageView *)softFrameAnimateWithImageName:(NSString *)imageName numberOfDigits:(NSInteger)digits firstDigit:(NSInteger)firstDigit andExtension:(NSString *)ext loop:(BOOL)loop loopCount:(NSInteger)loopCount andFPS:(CGFloat)framesPerSecond inView:(UIView *)view inPoint:(CGPoint)center{
39-
40-
NSString *format = [NSString stringWithFormat:@"%%@%%0%dd",(int)digits];
41-
NSString *imageNameFirst = [NSString stringWithFormat:format,imageName,firstDigit];
42-
NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageNameFirst ofType:ext];
38+
-(void)playIdle{
39+
if(!self.idleimagename)
40+
return;
4341

44-
UIImage *img = [UIImage imageWithContentsOfFile:imagePath];
45-
if(!img)
46-
return nil;
42+
[self softFrameAnimateWithImageName:self.idleimagename numberOfDigits:self.idlenumberOfDigits firstDigit:self.idlefirstdigit andExtension:self.idleextension loop:YES loopCount:0 andFPS:self.idlefps];
43+
}
44+
45+
-(void)resumeIdle{
46+
if(!self.idleimagename)
47+
return;
4748

48-
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, img.size.width/2.0, img.size.height/2.0)];
49-
imageView.image = img;
50-
imageView.center = center;
51-
[view addSubview:imageView];
52-
[imageView softFrameAnimateWithImageName:imageName numberOfDigits:digits firstDigit:firstDigit andExtension:ext loop:loop loopCount:loopCount andFPS:framesPerSecond];
49+
if([self.imagename isEqualToString:self.idleimagename]){
50+
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(loopImages) object:nil];
51+
52+
self.pause = NO;
53+
[self loopImages];
54+
}else{
55+
[self playIdle];
56+
}
57+
}
58+
59+
-(void)pauseIdle{
60+
if(!self.idleimagename)
61+
return;
5362

54-
return imageView;
63+
if([self.imagename isEqualToString:self.idleimagename]){
64+
[self pauseSoftFrameAnimation];
65+
}
66+
}
67+
68+
-(void)removeIdle{
69+
if(!self.idleimagename)
70+
return;
5571

72+
if([self.imagename isEqualToString:self.idleimagename]){
73+
[self pauseSoftFrameAnimation];
74+
}
75+
self.idleimagename = nil;
5676
}
5777

78+
79+
80+
#pragma mark - SoftFrameAnimations Soft animate
81+
5882
-(void)softFrameAnimateWithImageName:(NSString *)imageName numberOfDigits:(NSInteger)digits firstDigit:(NSInteger)firstDigit andExtension:(NSString *)ext loop:(BOOL)loop loopCount:(NSInteger)loopCount andFPS:(CGFloat)framesPerSecond{
5983
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(loopImages) object:nil];
6084

@@ -121,6 +145,8 @@ -(void)loopImages{
121145
}
122146
}
123147

148+
self.imagename = nil;
149+
124150
return;
125151
}
126152

@@ -140,6 +166,8 @@ -(void)loopImages{
140166
}
141167
}
142168

169+
self.imagename = nil;
170+
143171
if(self.idleimagename){
144172
[self softFrameAnimateWithImageName:self.idleimagename numberOfDigits:self.idlenumberOfDigits firstDigit:self.idlefirstdigit andExtension:self.idleextension loop:YES loopCount:0 andFPS:self.idlefps];
145173
}
@@ -177,13 +205,27 @@ -(void)pauseSoftFrameAnimation{
177205
self.pause = YES;
178206
}
179207

180-
-(void)playSoftFrameAnimation{
208+
-(void)stopSoftFrameAnimation{
209+
if(!self.imagename)
210+
return;
211+
212+
[self pauseSoftFrameAnimation];
213+
self.imagename = nil;
214+
}
215+
216+
-(void)resumeSoftFrameAnimation{
217+
if(!self.imagename)
218+
return;
219+
181220
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(loopImages) object:nil];
182221

183222
self.pause = NO;
184223
[self loopImages];
185224
}
186225

226+
227+
#pragma mark - SoftFrameAnimations Image setting
228+
187229
-(void)setImagePath:(NSString *)image{
188230
if(!self.continuePlayingOnSetImage){
189231
[self pauseSoftFrameAnimation];
@@ -192,11 +234,36 @@ -(void)setImagePath:(NSString *)image{
192234
[self setImage:[UIImage imageWithContentsOfFile:image]];
193235
}
194236

237+
238+
#pragma mark - SoftFrameAnimations Current image name
239+
195240
-(NSString *)getImageNameDisplayed{
196241
return self.imageDisplayed;
197242
}
198243

199244

245+
#pragma mark - SoftFrameAnimations Static image creation
246+
247+
+(UIImageView *)softFrameAnimateWithImageName:(NSString *)imageName numberOfDigits:(NSInteger)digits firstDigit:(NSInteger)firstDigit andExtension:(NSString *)ext loop:(BOOL)loop loopCount:(NSInteger)loopCount andFPS:(CGFloat)framesPerSecond inView:(UIView *)view inPoint:(CGPoint)center{
248+
249+
NSString *format = [NSString stringWithFormat:@"%%@%%0%dd",(int)digits];
250+
NSString *imageNameFirst = [NSString stringWithFormat:format,imageName,firstDigit];
251+
NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageNameFirst ofType:ext];
252+
253+
UIImage *img = [UIImage imageWithContentsOfFile:imagePath];
254+
if(!img)
255+
return nil;
256+
257+
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, img.size.width/2.0, img.size.height/2.0)];
258+
imageView.image = img;
259+
imageView.center = center;
260+
[view addSubview:imageView];
261+
[imageView softFrameAnimateWithImageName:imageName numberOfDigits:digits firstDigit:firstDigit andExtension:ext loop:loop loopCount:loopCount andFPS:framesPerSecond];
262+
263+
return imageView;
264+
265+
}
266+
200267

201268

202269
#pragma mark - Getters and setters

README.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,121 @@
99

1010
To run the example project, clone the repo, and run `pod install` from the Example directory first.
1111

12+
UIImageViewSoftFrameAnimations is a powerfull framework to run frame animations without having to load all images in memory at the beginning of the animation.
13+
You just have to provide the name of the sequense, the numbero of digits of the sequence and the starting number. With this, the frameworks does everything.
14+
15+
The main function is the following:
16+
17+
```
18+
-(void)softFrameAnimateWithImageName:(NSString *)imageName
19+
numberOfDigits:(NSInteger)digits
20+
firstDigit:(NSInteger)firstDigit
21+
andExtension:(NSString *)ext
22+
loop:(BOOL)loop
23+
loopCount:(NSInteger)loopCount
24+
andFPS:(CGFloat)framesPerSecond;
25+
```
26+
27+
If we take as an example the following frames:
28+
bird_01.jpg
29+
bird_02.jpg
30+
bird_03.jpg
31+
bird_04.jpg
32+
...
33+
bird_13.jpg
34+
bird_14.jpg
35+
36+
`imageName` is the bird_ part
37+
`digits` is 2 because there are only two digits in all the sequence frames.
38+
`firstDigit` is 1
39+
`ext` is jpg without the point
40+
`loop` will determine if the animation will loop.
41+
`loopCount` if you have specified loop = YES, loopCount will be the number of times that the animation will be reproduced. If loop is NO, this parameter will be ignored.
42+
`framesPerSecond` is the speed of the animation
43+
44+
If you want to play an animation only one time, you can achieve this in two ways:
45+
46+
1 - loop = NO
47+
2 - loop = YES and loopCount = 1
48+
49+
The two ways make differents things. The first one will reproduce the animation once and it will leave the last frame as final image. The second one will leave the first frame as the final image. It will give you the opportunity to achieve different animations only changing two parameters.
50+
51+
52+
You can pause the animation at anytime:
53+
54+
```
55+
-(void)pauseSoftFrameAnimation;
56+
```
57+
58+
And resume it:
59+
60+
```
61+
-(void)resumeSoftFrameAnimation;
62+
```
63+
64+
65+
- Idle animations
66+
67+
The framework gives the capacity to play idle animations:
68+
69+
```
70+
-(void)setIdleAnimation:(NSString *)idleAnimationName
71+
numberOfDigits:(NSInteger)digits
72+
firstDigit:(NSInteger)firstDigit
73+
andExtension:(NSString *)ext
74+
startNow:(BOOL)startNow
75+
andFPS:(CGFloat)framesPerSecond;
76+
```
77+
78+
You can configure an idle image and specify if you want to play it now or not.
79+
80+
When the idle image is setted and you play a softFrameAnimation with loop = NO, when the animation has ended, the idle animation will continue playing.
81+
82+
You can always play the idle manually:
83+
84+
```
85+
-(void)playIdle;
86+
```
87+
88+
And also remove the idle
89+
90+
```
91+
-(void)removeIdle;
92+
```
93+
94+
95+
- Dynamic animations
96+
97+
As an additive, the framework give you the oportunity to create and add an animation into a view with one single line of code:
98+
99+
```
100+
+(UIImageView *)softFrameAnimateWithImageName:(NSString *)imageName
101+
numberOfDigits:(NSInteger)digits
102+
firstDigit:(NSInteger)firstDigit
103+
andExtension:(NSString *)ext
104+
loop:(BOOL)loop
105+
loopCount:(NSInteger)loopCount
106+
andFPS:(CGFloat)framesPerSecond
107+
inView:(UIView *)view
108+
inPoint:(CGPoint)center;
109+
```
110+
111+
With the delegates you can know when the animation has ended and remove the UIImageView from the view if you want.
112+
113+
114+
- Delegates
115+
116+
The framework also provides some delegates where you can listen for some events:
117+
118+
```
119+
-(void)softFrameAnimation:(UIImageView *)softFrameAnimationView didShowFrame:(NSInteger)frame;
120+
-(void)softFrameAnimationDidEndLoop:(UIImageView *)softFrameAnimationView;
121+
-(void)softFrameAnimationDidEndAnimation:(UIImageView *)softFrameAnimationView;
122+
-(void)softFrameAnimation:(UIImageView *)softFrameAnimationView didSetImage:(NSString *)image;
123+
```
124+
125+
Enjoy!
126+
12127
## Requirements
13128

14129
## Installation

0 commit comments

Comments
 (0)