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...
}
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 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]];
}
If I create a UIImageView via Storyboard and add it as a #property into a ViewController.h, I can access that UIImageView from anywhere in the ViewController.m via [self UIImageView] or self.UIImageView.
But If I create the UIImageView programmatically from viewDidLoad as you see below, I seem to lose the ability to access it from outside viewDidLoad.
UIImageView *prevImgView = [[UIImageView alloc] init];
UIImageView *currImgView = [[UIImageView alloc] init];
UIImageView *nextImgView = [[UIImageView alloc] init];
NSArray *imageViews = [NSArray arrayWithObjects:prevImgView, currImgView, nextImgView, nil];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[self.view addSubview: scrollView];
CGRect cRect = scrollView.bounds;
UIImageView *cView;
for (int i=0; i<imageViews.count; i++) {
cView = [imageViews objectAtIndex:i];
cView.frame = cRect;
[scrollView addSubview:cView];
cRect.origin.x += cRect.size.width;
}
So later on if I want to modify the image that is being displayed in any one of the UIImageView I've created from viewDidLoad, I can't seem to access it. Does anyone know what I'm doing wrong?
You would need to create your UIImageView in your header file and then it would be available throughout the rest of the view. You will need to add it to the view in viewDidLoad though.
Header
UIImageView *image;
Main // ViewDidLoad
image = [UIImageView alloc] .....
[self.view addSubView image];
Main Function .....
[image setImage ....
The answer AgnosticDev is trying to say here is to add an ivar to your view controller's .h file... e.g.:
#implementation YHLViewController : ViewController
{
UIImageView * currImageView;
}
And then in your "viewDidLoad" method, you can allocate and initialize it via:
currImageView = [[UIImageView alloc] init];
and have access to it from anywhere else in your view controller.
I've started from the standard PageView-based application template provided by Apple. Now instead of text I want the pages in the app to display images. An array is created as follows:
_pageData = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
nil];
This array is handled the following way:
DataViewController *dataViewController = [storyboard instantiateViewControllerWithIdentifier:#"DataViewController"];
dataViewController.dataObject = [self.pageData objectAtIndex:index];
return dataViewController;
Then, in DataViewController.h:
#interface DataViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIImageView *imageView;
#property (strong, nonatomic) UIImage *dataObject;
And in DataViewController.m I'm trying to display the images in de imageView in the following way:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.imageView = [[UIImageView alloc] initWithImage:_dataObject];
NSLog(#"dataObject %#", _dataObject);
}
However, with this code I'm not succeeding in getting the images onto the PageViewController. Any idea how I could get this working properly?
A big thanks for you help!
Here
self.imageView = [[UIImageView alloc] initWithImage:_dataObject];
you are creating a new UIImageView that is dangling arround unused, invisible and without a parent.
You need to set it's size somehow and put it to a parent view with a
[<parent> addSubview:self.imageView]
to show it.
If your original UIImageView is read from a resource file ( I assume so because it is a IBOutlet ),
just do a
self.imageView.image = _dataObject
and this should do the job instead.
This worked for me
NSArray *anArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"transparente.png"],
[UIImage imageNamed:#"21001.jpg"],
[UIImage imageNamed:#"21002.jpg"],
[UIImage imageNamed:#"21003.jpg"],
[UIImage imageNamed:#"21004.jpg"],
[UIImage imageNamed:#"transparente2.png"],
nil];
_pageData = [anArray copy];
and in the dataViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.pageImage.image = self.dataObject;
}
The pageImage is an UIImageView with an IBOutlet.