I am trying to change an image to another random image when a button is pressed.
I have uploaded 5 images to my "Supporting Files" and I now need to reference them.
I need to create an array which holds all of the images.
I create a button that will run a method when is clicked.
This method will choose a random number of the array and then display that image.
Can someone please give me some sample code for the 3 things I need to do.
(Please note I will also be needing to display text underneath the images in a label field and it will have to be linked to the image being displayed)
Thanks
take an array like this in viewDidLoad method,take three global variables imagesArray,titlesArray and
imageview
imagesArray = [NSArray alloc]initWithObjects:#"image1.png",#"image2.png",#"image3.png",#"image4.png",#"image5.png" nil];
titlesArray = [NSArray alloc]initWithObjects:#"title1",#"title2",#"title3",#"title4",#"title5" nil];
-(IBAction)randomImage:(id)sender{
int n= arc4random() % [imagesArray count];
imageview.image= [UIImage imageNamed:[imagesArray objectAtIndex:n]];
label.text=[titlesArray objectAtIndex:n];
}
Try this
NSMutableArray * imagenames = [[NSMutableArray alloc] initWithObjects:#"image1.png",#"image2.png",#"image3.png",#"image4.png",#"image5.png", nil];
NSMutableArray * Images = [[NSMutableArray alloc] init];
for(int i=0;i<imagenames.count;i++)
{
UIImage * image = [UIImage imageNamed:[imagenames objectAtIndex:i]];
[Images addObject:image];
}
int n= arc4random() % 5;
if(n<Images.count)
{
UIImageView * imageView = [[UIImageView alloc] initWithImage:[Images objectAtIndex:n]];
}
Related
I am using the below code to change the UIImageView's image after a short duration. The images being used are stored in an array as you can see.
The problem is, no matter what 'duration' or 'delay' I set, the imageview changes almost instantly to the last image in the array.
Should I instead be using the main thread to add a delay between each image transition?
////////////////////////////////////////////////////////////////
animationCount = 0;
imageArray =[NSMutableArray new];
[imageArray addObject:[UIImage imageNamed:#"gauge1final.png"]];
[imageArray addObject:[UIImage imageNamed:#"gauge2final.png"]];
[imageArray addObject:[UIImage imageNamed:#"gauge3final.png"]];
[imageArray addObject:[UIImage imageNamed:#"gauge4final.png"]];
[imageArray addObject:[UIImage imageNamed:#"gauge5final.png"]];
[self multiStageAnimate];
////////////////////////////////////////////////////////////////
-(void) multiStageAnimate{
[UIView animateWithDuration:5.0
delay:5.0
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^
{
self.gauge.image = [imageArray objectAtIndex:animationCount];
}
completion:^(BOOL finished){
animationCount ++;
if(animationCount < imageArray.count){
[self multiStageAnimate];
}
else
{
animationCount = 0;
}
}];
}
UIImageView does have a mechanism to accomplish your need.
UIImageView *imageView = [[UIImageView alloc] init];
imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1"],
[UIImage imageNamed:#"image2"],
[UIImage imageNamed:#"image3"], nil];
imageView.animationRepeatCount = 0; // 0 means repeat without stop.
imageView.animationDuration = 1.5; // Animation duration
[imageView startAnimating];
If you are using multiple screens with the same animation, it's better to create one .plist file and then use this anywhere in your project. The steps are as follows:
First, generate one .plist file File-> New File-> Resource ->Property List
Write your code like this:
ViewController.h
#property (weak, nonatomic) IBOutlet UIImageView *dataImageView;
ViewController.m
NSDictionary *picturesDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"PhotesArray" ofType:#"plist"]];
NSArray *imageNames = [picturesDictionary objectForKey:#"Photos"];
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i=0; i<[imageNames count]; i++) {
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
//Normal animation
self.dataImageView.animationImages = images;
self.dataImageView.animationDuration = 5.0;
[self.dataImageView startAnimating];
The image property of a UIImageView won't animate. What you need to do is have 2 UIImageView widgets one on top of another. Then, you can animate their alpha properties from 0 to 1 (and vice-versa) with each transition.
I have 5 objects that i want to assign to UIImageViews named "obstacle1","obstacle2",etc. how can i do this in a for loop somewhat like this...
for(int i=0;i<5;i++)
{
UIImageView "obstacle%d",i = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"square.png"]];
}
//the <"obstacle%d",i> part is what i need help with
You should really use an Array for holding your UIImageView objects:
NSMutableArray *imageViews = [NSMutableArray array];
for(int i=0;i<5;i++)
{
UIImageView *anObstacle = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"square%d.png",colorPick]]];
[imageViews addObject:anObstacle];
}
Then you can just access them in the array by doing imageViews[0].
I want to add/delete images in a view using a UIStepper. So every time you press the '+' the same image is copied within the same viewController, still being able to move/rotate every copy.
I have something like this:
- (IBAction)ValueChanged:(UIStepper *)value
{
NSMutableArray *imageArray = [[NSMutableArray alloc] init];
int j = (int) value.value;
for( int i = 0 ; i < j ; ++i )
{
UIImageView *newImageView = [[UIImageView alloc] init];
newImageView.image = self.myImageView.image;
[imageArray addObject:newImageView];
[self.view addSubview:imageArray[i]];
}
}
but this does not work because it just replaces the previous image.
Is there a proper way to copy the same image multiple times using the UIStepper?
I have created an array of images and they start in an imageview. The user can pick up the images using a gesture and drag and drop them in a separate imageview. They can move them about back and forth and they shuffle and organise themselves and all is well.
But I have no idea which ones they have moved into which imageview. How do I identify which image lives in which imageview. Can I query what is in an imageview?
I looked at tags and that didn't help much.
I generate array as so
NSArray *pngs = [NSArray arrayWithObjects:#"red", #"blue", #"green", #"yellow", #"purple", #"orange", #"black", #"white", nil];
for (NSString *png in pngs) {
UIImage *draggableImage = UIImageWithBundlePNG(png);
UIImageView *draggableImageView = [[UIImageView alloc] initWithImage: draggableImage];
SEDraggable *draggable = [[SEDraggable alloc] initWithImageView: draggableImageView];
[self configureDraggableObject: draggable];
Not sure where to even start.
try adding tag to UIImageView
NSArray *pngs = [NSArray arrayWithObjects:#"red", #"blue", #"green",
#"yellow", #"purple", #"orange", #"black", #"white", nil];
for (NSString *png in pngs) {
UIImage *draggableImage = UIImageWithBundlePNG(png);
UIImageView *draggableImageView = [[UIImageView alloc] initWithImage: draggableImage];
draggableImageView.tag = i++;//i must be initialized
SEDraggable *draggable = [[SEDraggable alloc] initWithImageView: draggableImageView];
[self configureDraggableObject: draggable];
you can identify the imageview by [imageView viewWithTag:TAG]; method.
To know the the view when a view has been attached you need just to look the property 'superView' so in your case if want to know the view that has inside the UIImageView, you need to have a reference to the UIImageView like:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bla"]];
UIView *imageContainer = imageView.superView;
if you assign a tag to your container now you are able to do that:
if (imageContainer.tag == aTag) {
Do something...
}
I have this simple function where i sap rate the button animation and then start animation for every button but i dont know why only one button animate not others which clicked, please help me with this
- (IBAction)startAnimation:(UIButton *)button {
NSMutableArray* imagesArray = [[NSMutableArray alloc] init];
for (int images = 0; images < 15; images++) {
UIImage* buttonImage = [UIImage imageNamed:[NSString stringWithFormat:#"aaqqq00%02d.png", images]];
[imagesArray addObject:buttonImage];
}
NSArray* reversedAnim = [[imagesArray reverseObjectEnumerator] allObjects];
int buttonTag = button.tag;
animButton.adjustsImageWhenHighlighted = NO;
for (int images = 0; images < 15; images++) {
UIButton *animButton = (UIButton *)[self.view viewWithTag:buttonTag];
if (images <= buttonTag) {
animButton.imageView.animationImages = imagesArray;
[animButton setImage:
[UIImage imageNamed:#"aaqqq0014.png"] forState:UIControlStateNormal];
} else {
animButton.imageView.animationImages = reversedAnim;
[animButton setImage:
[UIImage imageNamed:#"aaqqq0000.png"] forState:UIControlStateSelected];
}
NSLog(#"%#", animButton.imageView.animationImages);
animButton.imageView.animationDuration = 1; //whatever you want (in seconds)
animButton.imageView.animationRepeatCount = 1;
[animButton.imageView startAnimating];
}
}
You're passing the button, so doing the tag lookup seem pointless and error prone (if there are multiple views with the same tag in the hierarchy).
Setting the buttons image view animation images (animButton.imageView.animationImages) and then after that setting a single image ([animButton setImage:...) is also pointless.
What does your log statement say about the animation images?