UIImageView animation loops with different speeds - iphone

I've got an array with several images that I'm adding into an UIImageView:
myImageView.animationImages = myArrayOfImages;
Now, I want to execute that loop of images X times, but each one with a different duration
myImageView.animationDuration = randomTime;
Is this possible?

Have you tried? I believe you will need to create a new instance of the UIImageView each time.

Related

Looping through images using SpriteWidget

I'm using SpriteWidget in Flutter.
https://github.com/spritewidget/spritewidget
I'm loading images like this:
// Load multiple images
await images.load([
'assets/image_0.png',
'assets/image_1.png',
'assets/image_2.png',
]);
// Access a loaded image from the ImageMap
var image0 = images['assets/image_0.png'];
I'm trying to figure out how to loop through these images so that they are like an animated gif. Show image_0 for half a second, then image_1 for half a second then image_2 for half a second and so on.
I understand that the usual and recommended way to do this is with a spritesheet, but I do not have a sprite sheet, only individual images.
How about using Sequences? You can loop over an array of images based on time.
https://github.com/spritewidget/spritewidget#sequences

how to make "Farm Flip - Memory Match for Kids" like memory based game

i'm about to developp a memory game for children like,i have 6 different images show in in random order in 12 places. note(each image show twice) and i want to know how to set images randomly and how to mach UIImage with animation? any help..
Have a look at the answer by Kristopher at What's the Best Way to Shuffle an NSMutableArray?. You can create an array of 12 images (each repeated twice) & then shuffle the array. Then display the images in the order obtained after shuffling.
Regarding animations, you first need to at least visualize what animation you want. People can then help you achieve that.
HTH,
Akshay

Simple animation as iphone app background

I want to make an animated background for iphone app. Something simple 5-6 frames changing in the loop. On the front there will be another animation running. How can this be done?
The easiest thing to do is probably to use the animationImages property of a UIImageView. Once you have the animationImages property correctly set up, just call startAnimating on your view. So your code would look something like:
imageView.animationImages = myNSArrayofUIImagesObjects;
imageView.animationDuration = 1; // by default this is equal to the number of images multiplied by 1/30th of a second
[imageView startAnimating];
An important thing to note is that you can't easily control how long each image is shown. But what you can do is use the same image in your NSArray of images multiple times. So, for example, you could have an NSArray of length 500, where the first 100 entries map to your first image, the second 100 entries map to your second image, etc. Make sure to minimize the amount of memory you're loading onto the heap by reusing the same UIImage object for each of your five or six images.

How do I play a specific range of a UIImageView animation sequence array?

I have a question regarding a png, animated image sequence.
I am loading images from 1 - 35 into a layer and I am initializing the layer with the 1st image using - initWithImage. I have several buttons that want to be able to play different ranges of the image sequence array.
Is there a way to play only the images from 1 - 10?
Is there a way to control the range of images played... 11 - 35 or 4 - 20 or whatever?
Of course I tried creating separate layers with separate array's of images and having the layers on top of each other. The problem their is the initWithImage. If I play the second layer I can see the initialized image underneath.
Here is my image sequence code:
- (void) loadAnim05 {
cAnim = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cof_01.png"]];
cAnim.center = CGPointMake(78,211);
NSMutableArray *array = [NSMutableArray array];
for (int i = 1; i <= 35; i++)
[array addObject:[UIImage imageNamed:[NSString stringWithFormat:#"cof_%02d.png",i]]];
cAnim.animationImages = array;
cAnim.animationDuration = 1.0;
cAnim.animationRepeatCount = 1;
[self.view addSubview:cAnim];
[cAnim release];
}
Thank you very much for any help.
Why don't you maintain an array of images elsewhere and use -subarrayWithRange: method of the NSArray object to pick the images that you want to animate and assign it to the animationImages property of your imageView.
But I don't think UIImageView provides you with mechanism to play a subarray of animationImages.
I am in the process of writing a little animation framework to do exactly this. You can find the code at http://github.com/st3fan/iphone-animation
Key features are:
Optimized for PNG images with not too many shapes on a plain background
Includes a tool to convert a PNG sequence (like exported from Flash) to a simple one-file container format
Keeps frames compressed in memory. Decompresses realtime.
Frames are compressed with a simple run-length encoding
Typical full screen animation takes ~ 10% CPU
I wrote this specifically for games in the http://tickletapapps.com collection. It works very well for the kind of animations used in those games. Contact me via Github if you need help. The code is work in progress but working pretty well.

Reusing subview

I have a UIImageView object that is just a plain black rectangle.
This is what i use to select a button in the view.
Problem is, I have 49 of these buttons in my view, and all of them can be selected at the same time.
What I use for adding a subview to a button is:
UIImageView* selectedSquareView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,40,40)];
[selectedSquareView setImage:[UIImage imageNamed:#"SelectedSquare.png"]];
[button addSubview: selectedSquareView];
I would like the selectedSquareView to be reused multiple times as subviews for the other buttons, but only keep one allocation of it. I would prefer not having 49 UIImageViews created at the same time just for this purpose. Is this possible?
If not, should I store them in a NSMutableArray for easy removal later?
Regards
-Raymond
You will need 49 UIImageViews, you only need 1 UIImage. The UIImageViews contain position, size, is_higlighted, etc information for each button.
That being said even if you had a lot of UIImage's, UIImage is supposed to be pretty intelligent about these things as Apple describes in their documentation:
In low-memory situations, image data may be purged from a UIImage object to free up memory on the system. This purging behavior affects only the image data stored internally by the UIImage object and not the object itself. When you attempt to draw an image whose data has been purged, the image object automatically reloads the data from its original file. This extra load step, however, may incur a small performance penalty.
You should avoid creating UIImage objects that are greater than 1024 x 1024 in size. Besides the large amount of memory such an image would consume, you may run into problems when using the image as a texture in OpenGL ES or when drawing the image to a view or layer. This size restriction does not apply if you are performing code-based manipulations, such as resizing an image larger than 1024 x 1024 pixels by drawing it to a bitmap-backed graphics context. In fact, you may need to resize an image in this manner (or break it into several smaller images) in order to draw it to one of your views.
Alternatively if you really feel like you need to do delete the UIImageView's when not in use you can do as you suggest store them in an array and release them on viewDidDisappear and then recreate them all on viewWillAppear.
Each UIView appears only once, so you will definitely need to create 49 copies of it.
Your current code is probably fine, since UIImage will probably cache the image, but you might like to create the image only once and then set it each time, something like:
UIImageView* selectedSquareView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,40,40)];
static UIImage* kSelectedSquareImage = [UIImage imageNamed:#"SelectedSquare.png"] retain];
[selectedSquareView setImage:kSelectedSquareImage];
If not, should I store them in a
NSMutableArray for easy removal later?
It depends if there are any other views in the container view - if not, then there is no need to store them in an NSMutableArray as you can just use container.subviews to get an array of views. Otherwise, sure, you could store them in an NSMutableArray and remove them that way (just make sure you remove them from the array or release the array as well, otehrwise they will remain in memory simply because they are stored in the array).