Simple Animations [iPhone] - iphone

I have a few UIButtons that I'd like to trigger short 5-10 frame animations when pressed in the middle of the screen along with other nonrelated stuffs. Can I just drop one UIImageView in IB and update it, somehow, programmatically? It doesn't need to be overly complex, I don't even mind if it's hard coded, for now since it's such a small project.
Thanks.

Here's an example of the easiest way you do an animation with just a few frames is:
Declare an IBOutlet UIImageView *timerAnimation and link it to your UIIMageView in IB. Add some number of images to your resources, such as you see needed below. Then load the images into an array - see the code:
timerAnimation.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"timer00.png"],
[UIImage imageNamed:#"timer01.png"],
[UIImage imageNamed:#"timer02.png"],
[UIImage imageNamed:#"timer03.png"],
[UIImage imageNamed:#"timer04.png"],
[UIImage imageNamed:#"timer05.png"],
[UIImage imageNamed:#"timer06.png"],
[UIImage imageNamed:#"timer07.png"],
[UIImage imageNamed:#"timer08.png"],
[UIImage imageNamed:#"timer09.png"],
[UIImage imageNamed:#"timer10.png"],
[UIImage imageNamed:#"timer11.png"],
[UIImage imageNamed:#"timer12.png"],
[UIImage imageNamed:#"timer13.png"],
[UIImage imageNamed:#"timer14.png"],
[UIImage imageNamed:#"timer15.png"],
[UIImage imageNamed:#"timer16.png"],
[UIImage imageNamed:#"timer17.png"],
[UIImage imageNamed:#"timer18.png"],
[UIImage imageNamed:#"timer19.png"],
[UIImage imageNamed:#"timer20.png"],
[UIImage imageNamed:#"timer21.png"],
[UIImage imageNamed:#"timer22.png"],
[UIImage imageNamed:#"timer23.png"],
[UIImage imageNamed:#"timer24.png"],
[UIImage imageNamed:#"timer25.png"],
[UIImage imageNamed:#"timer26.png"],
[UIImage imageNamed:#"timer27.png"],
[UIImage imageNamed:#"timer28.png"],
[UIImage imageNamed:#"timer29.png"],
[UIImage imageNamed:#"timer30.png"],
[UIImage imageNamed:#"timer31.png"],
[UIImage imageNamed:#"timer32.png"],
[UIImage imageNamed:#"timer33.png"],
[UIImage imageNamed:#"timer34.png"],
[UIImage imageNamed:#"timer35.png"],
// [UIImage imageNamed:#"timer36.png"], save last picture for "times up"
nil];
timerAnimation.animationDuration = 5.0;
timerAnimation.animationRepeatCount = 1;
[timerAnimation startAnimating];
This will run your animation until you call [timerAnimation stopAnimating];

Yes. Create an IBOutlet to your UIImageView and you can update it from code. For example, use its image property to set the UIImage that should be displayed. Or you can add the image to your app's resources and associate it with the UIImageView in Interface Builder. Use .png files since the iPhone has been optimized for them.
Since a UIImageView is a UIView, all the usual Core Animation methods can be used.

Related

How to add array of images to UIImage view programmatically in ios4?

I need to display array of images in a single view which have various effect in icarousel(time machne,coverflow,etc...)i'm going to use single style where all images have to be displayed..
HERE IS MY CODE IN .M FILE..
UIImage *img=[[UIImageView alloc]initWithFrame:CGRectMake(30, 30, 290, 400)];
NSLog(#"hi");
img.image=[NSArray arrayWithObjects:[UIImage imageNamed:#"Cover_0.png"],
[UIImage imageNamed:#"Cover_1.png"],
[UIImage imageNamed:#"Cover_2.png"],
[UIImage imageNamed:#"Cover_3.png"],
[UIImage imageNamed:#"Cover_4.png"],
[UIImage imageNamed:#"Cover_5.png"],
[UIImage imageNamed:#"Cover_6.png"],
[UIImage imageNamed:#"Cover_7.png"],nil];
But its not working...Can anyone help it out...
Update: As per your latest edit, your question entirely changed, hence my answer does not apply.
Use animationImages instead:
UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(30, 30, 290, 400)];
img.animationImages=[NSArray arrayWithObjects:[UIImage imageNamed:#"Cover_0.png"],
[UIImage imageNamed:#"Cover_1.png"],
[UIImage imageNamed:#"Cover_2.png"],
[UIImage imageNamed:#"Cover_3.png"],
[UIImage imageNamed:#"Cover_4.png"],
[UIImage imageNamed:#"Cover_5.png"],
[UIImage imageNamed:#"Cover_6.png"],
[UIImage imageNamed:#"Cover_7.png"],nil];
From the UIImageView documentation;
animationImages
An array of UIImage objects to use for an animation.
#property(nonatomic, copy) NSArray *animationImages
Discussion The array must contain UIImage objects. You may use the
same image object more than once in the array. Setting this property
to a value other than nil hides the image represented by the image
property. The value of this property is nil by default.
Availability
Available in iOS 2.0 and later.
See Also
#property image
#property contentMode (UIView)
Declared In
UIImageView.h
Have you tried
NSArray *frames = [NSArray arrayWithObjects:
[UIImage imageWithName:#"a.png"],
[UIImage imageWithName:#"b.png"],
[UIImage imageWithName:#"c.png"],
[UIImage imageWithName:#"d.png"],
[UIImage imageWithName:#"e.png"],
nil];
UIImageView *animatedIMvw = [[UIImageView alloc] init];
animatedIMvw.animationImages = frames;
[animatedIMvw startAnimating];
Check out this simple example: http://appsamuck.com/day2.html
EDIT
It appears that the homepage got broken, so check this out instead:
http://web.archive.org/web/20090207210729/http://appsamuck.com/day2.html

how to distribute all image with poper tag

i try to create multiple image array and now i want to add proper tag with specific deck like(Hearts,Diamonds,Spades,Clubs). so in image name as C=dimond, R=Heart,K=spades,F=Clubs and image no as 11=Jack,12=Queen,13=King...so how to distribute it with proper tag so i can indentify specific image.
->>>>>>>>
imagearr =
[[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"C1.png"],
[UIImage imageNamed:#"C2.png"],
[UIImage imageNamed:#"C3.png"],
[UIImage imageNamed:#"C4.png"],
[UIImage imageNamed:#"C5.png"],
[UIImage imageNamed:#"C6.png"],
[UIImage imageNamed:#"C7.png"],
[UIImage imageNamed:#"C8.png"],
[UIImage imageNamed:#"C9.png"],
[UIImage imageNamed:#"C10.png"],
[UIImage imageNamed:#"C11.png"],
[UIImage imageNamed:#"C12.png"],
[UIImage imageNamed:#"C13.png"],
[UIImage imageNamed:#"F1.png"],
[UIImage imageNamed:#"F2.png"],
[UIImage imageNamed:#"F3.png"],
[UIImage imageNamed:#"F4.png"],
[UIImage imageNamed:#"F5.png"],
[UIImage imageNamed:#"F6.png"],
[UIImage imageNamed:#"F7.png"],
[UIImage imageNamed:#"F8.png"],
[UIImage imageNamed:#"F9.png"],
[UIImage imageNamed:#"F10.png"],
[UIImage imageNamed:#"F11.png"],
[UIImage imageNamed:#"F12.png"],
[UIImage imageNamed:#"F13.png"],
[UIImage imageNamed:#"K1.png"],
[UIImage imageNamed:#"K2.png"],
[UIImage imageNamed:#"K3.png"],
[UIImage imageNamed:#"K4.png"],
[UIImage imageNamed:#"K5.png"],
[UIImage imageNamed:#"K6.png"],
[UIImage imageNamed:#"K7.png"],
[UIImage imageNamed:#"K8.png"],
[UIImage imageNamed:#"K9.png"],
[UIImage imageNamed:#"K10.png"],
[UIImage imageNamed:#"K11.png"],
[UIImage imageNamed:#"k12.png"],
[UIImage imageNamed:#"K13.png"],
[UIImage imageNamed:#"R1.png"],
[UIImage imageNamed:#"R2.png"],
[UIImage imageNamed:#"R3.png"],
[UIImage imageNamed:#"R4.png"],
[UIImage imageNamed:#"R5.png"],
[UIImage imageNamed:#"R6.png"],
[UIImage imageNamed:#"R7.png"],
[UIImage imageNamed:#"R8.png"],
[UIImage imageNamed:#"R9.png"],
[UIImage imageNamed:#"R10.png"],
[UIImage imageNamed:#"R11.png"],
[UIImage imageNamed:#"R12.png"],
[UIImage imageNamed:#"R13.png"],nil];
..............
There's a better work around this issue rather than taking a bunch of images in the array. Initialize an Enum with all different deck categories and loading the image through dynamically generated name for e.g:
// In a header file
typedef enum {
HEARTS, // Hearts
DIAMONDS, // Diamonds
SPADES, // Spades
CLUBS // Club
} DeckType;
// In a source file
NSString * const DeckType_toString[] = {
#"H",
#"D",
#"S",
#"C"
};
- (UIImage *)getDeckImage:(DeckType)deckType withCardNo:(NSInteger)cardNo {
return [UIImage imageNamed:[NSString stringWithFormat:#"%#%d.png", DeckType_toString[deckType], cardNo]];
}
You can also take another enum with group of 1...13 cards and used them instead of cardNo
I am not sure,you could create a UIImage with tag .because it's inherited from NSObject NOT UIView which provide you an integer tag variable and used by the UIView class.
But still you could do it by writing a wrapper for you UIView class, and should be like below
use below as reference.
#interface MyUIImageWrapper: NSObject
{
UIImage* iMyImage;
int tag;
}
you could have some init function which take two argument one for image name and others for tag value
Like below
-(id) initWithTag:(NSString*) fileName withTag:(int) myTag ;
And the implementation for the method.
-(id) initWithTag:(NSString*) fileName withTag:(int) myTag
{
if(self = [super init])
{
iMyImage = [UIImage imageNamed:fileName];
tag = myTag;
}
return self;
}
To identify images in a collection I'm suggesting an NSDictionary instead of an NSArray so every image is associated with an NSString, e.g. [[NSDictionary alloc] initWithObjectsAndKeys:..., [UIImage imageNamed:#"R7.png"], #"R7", ...]; tagging works for UIImageViews (all UIView descendants) and is more proper for retrieving already added subviews...

gif image as a background

can we use a gif image as a background?
i want to set an gif image as a background, like some birds flying ....
can we use gif image to set as background.???
if yes than how?
thanks and regards
Yes you can - I assume you want an animated gif?
It's the same CSS rule as a static background.
body {
background: url(animated.gif);
}
Here, i got the solution.
UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
animatedImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"default1.jpg"],
[UIImage imageNamed:#"default2.jpg"],
[UIImage imageNamed:#"default3.jpg"],
[UIImage imageNamed:#"default4.jpg"],
[UIImage imageNamed:#"default5.jpg"],
[UIImage imageNamed:#"default6.jpg"],
[UIImage imageNamed:#"default7.jpg"],
[UIImage imageNamed:#"default8.jpg"],
[UIImage imageNamed:#"default9.jpg"],
[UIImage imageNamed:#"default10.jpg"],
[UIImage imageNamed:#"default11.jpg"],
[UIImage imageNamed:#"default12.jpg"],
[UIImage imageNamed:#"default13.jpg"],
[UIImage imageNamed:#"default14.jpg"],
[UIImage imageNamed:#"default15.jpg"], nil];
animatedImageView.animationDuration = 3.0f;
animatedImageView.animationRepeatCount = 0;
[animatedImageView startAnimating];
[self.view addSubview: animatedImageView];

Did OS4 change how the iPhone reads code?

The code I wrote for 3.2 performs differently in OS4. Still no errors, just new bugs.
So I'm initializing the imageView inside an IBAction called "randomize" that produces and random number that goes to one of 86 cases producing a resulting animation and image, the only difference in code between 0 and 86 is that final imageView.image (I just didn't want to past 86 copies of the same code in for you to have to wade through ). No matter what the case, the animation imageView.animationImages runs. The bug is that the first time I run the action, imageView.animationImages runs showing my animation, then instead of completing the code and going to the imageView.image = [UIImage imageNamed:#"image36.png"], it now just shows whatever I have set as the actual imageView background in Interface Builder. And Sometimes it runs the animation for one case and the imageview.image for another. Any ideas?
- (IBAction)randomize {
int Number = arc4random() % 86;
switch (Number) {
case 0:
imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"image2.png"],
[UIImage imageNamed:#"image3.png"],
[UIImage imageNamed:#"image4.png"],
[UIImage imageNamed:#"image5.png"],
[UIImage imageNamed:#"image6.png"],
[UIImage imageNamed:#"image7.png"],
[UIImage imageNamed:#"image8.png"],
[UIImage imageNamed:#"image9.png"],
[UIImage imageNamed:#"image10.png"],
[UIImage imageNamed:#"image0.png"],nil];
imageView.animationDuration = 0.50;
[imageView setAnimationRepeatCount: 1];
[imageView startAnimating];
imageView.image = [UIImage imageNamed:#"image36.png"];
break;
case 1:
imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"image2.png"],
[UIImage imageNamed:#"image3.png"],
[UIImage imageNamed:#"image4.png"],
[UIImage imageNamed:#"image5.png"],
[UIImage imageNamed:#"image6.png"],
[UIImage imageNamed:#"image7.png"],
[UIImage imageNamed:#"image8.png"],
[UIImage imageNamed:#"image9.png"],
[UIImage imageNamed:#"image10.png"],
[UIImage imageNamed:#"image0.png"],nil];
imageView.animationDuration = 0.50;
[imageView setAnimationRepeatCount: 1];
[imageView startAnimating];
imageView.image = [UIImage imageNamed:#"image37.png"];
break;
If I had to guess, you're missing break statments at random intervals in your 86 cases. Please consider refactoring to a method...

Freeze last frame of animation?

Is there a way to freeze the last frame of my animation in my iphone app? Here's my code so far.
popup.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"picture1.png"],
[UIImage imageNamed:#"picture2.png"],
[UIImage imageNamed:#"picture3.png"],
[UIImage imageNamed:#"picture4.png"],
[UIImage imageNamed:#"picture5.png"],
[UIImage imageNamed:#"picture6.png"],
[UIImage imageNamed:#"picture7.png"], nil];
popup.animationDuration = 1.750567;
popup.animationRepeatCount = 1;
[popup startAnimating];
[self.view addSubview:popup];
When animation stops UIImageView shows UIImage set in its image property. So you should just set it to the last frame of your animation:
popup.image = [UIImage imageNamed:#"picture7.png"];