
In iOS,animations are used extensively to relocate views, change their size, remove them from view hierarchies, and hide them.
You might use animations to convey feedback to the user or to implement interesting visual effects.
This is how a sprite can be animated to show a coin fall. I am going to share source code how to display coin drop animation.
You can implement these codes to represent your App with cool effects.
Create a new project in Xcode: File\New\New Project. Pick the Single View Application template and call it CoinDropAnimation.
Don't forget to check ARC and Storyboard. Even if in this tutorial we won’t be focusing on these new technologies, it is a good way to come across the new ideas.
Sprite.h
#import <UIKit/UIKit.h>
@interface Sprite : UIImageView {
NSUInteger uiValue;
NSInteger iValue;
CGFloat fValue;
}
@property (nonatomic, assign) NSUInteger uiValue;
@property (nonatomic, assign) NSInteger iValue;
@property (nonatomic, assign) CGFloat fValue;
@end
Sprite.m
#import "Sprite.h"
@implementation Sprite
@synthesize iValue, uiValue, fValue;
- (id)initWithImage:(UIImage *)image {
if ((self = [super initWithImage:image])) {
// Initialization code
}
return self;
}
@end
SpriteHelpers.h
#import <Foundation/Foundation.h>
#import "Sprite.h"
@interface SpriteHelpers : NSObject {
}
+ (Sprite *)setupAnimatedSprite:(id)sender numFrames:(NSInteger)frames withFilePrefix:(NSString *)filePrefix withDuration:(CGFloat)duration ofType:(NSString *)ext withValue:(NSInteger)val;
@end
SpriteHelpers.m
#import "SpriteHelpers.h"
#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
@implementation SpriteHelpers
+ (Sprite *)setupAnimatedSprite:(id)sender numFrames:(NSInteger)frames withFilePrefix:(NSString *)filePrefix withDuration:(CGFloat)duration ofType:(NSString *)ext withValue:(NSInteger)val {
// Create an array for for holding the frames of the animation
NSMutableArray *spriteArray = [[NSMutableArray alloc] initWithCapacity:8];
// Just like above but instead loop back down and add the images in reverse except drop the first and last frames
for(int i =frames; i > 1; i--){
UIImage *sprite = [UIImage imageNamed:[NSString stringWithFormat:@"%@%d.%@",filePrefix,i,ext]];
[spriteArray addObject:sprite];
}
int k=arc4random()%6;
// Create a new Sprite and init with the first image of our spriteArray (doesn't matter what image)
Sprite *spriteViewTemp = [[Sprite alloc] initWithImage:[spriteArray objectAtIndex:k]];
// This nullifies what image we added above and instead sets up an animated Sprite with the contents of our spriteArray
[spriteViewTemp setAnimationImages:spriteArray];
// Set the duration, or animation speed of our Sprite
[spriteViewTemp setAnimationDuration:duration];
// Without this, the animation would be stuck on frame1
// [spriteViewTemp startAnimating];
Sprite *tempImageView = spriteViewTemp;
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
spriteViewTemp.center = CGPointMake(900,(arc4random()%(NSUInteger)(1000 - tempImageView.image.size.width)) + (tempImageView.image.size.width / 2));
}else if (IS_IPHONE_5){
spriteViewTemp.center = CGPointMake(490,(arc4random()%(NSUInteger)(560 - tempImageView.image.size.width)) + (tempImageView.image.size.width / 2));
}else{
spriteViewTemp.center = CGPointMake(400,(arc4random()%(NSUInteger)(470 - tempImageView.image.size.width)) + (tempImageView.image.size.width / 2));
}
// Set the position of our Sprite
// Add the Sprite to the view sent in 'sender'
[sender addSubview:spriteViewTemp];
// Clean up
// Set the integer value of the Sprite (this is optional, depends if your app needs it)
[tempImageView setIValue:val];
// Finally, return the new animated Sprite
return tempImageView;
}
@end

coindropanimation.zip |