i have a UIImageView object that i'm trying to get x coordinate of. I do this with code below
endingPoint.x = myObject.center.x;
Now, how can i do this if i have the same UIImageView inside of an array that i'm trying to loop through and get each object's x coordinate, some thing like below?
endingPoint.x = [posArray objectAtIndex:i].center.x;
I know it's a newbie question but i'm just starting with iOS.
You're very close. objectAtIndex: however, returns objects of type id (a generic pointer to anything), so you may not call .center (property) on it.
You must send it a message using the brackets symbols like this:
endingPoint.x = [[posArray objectAtIndex:i] center].x;
or cast the value to (UIImageView *) first:
endingPoint.x = ((UIImageView *)[posArray objectAtIndex:i]).center.x;
This would be done inside a for loop obviously.
You can do it either in an Objective-C fast enumeration loop or in a standard for loop.
Fast enumeration loop would look something like this.
for (UIImageView *image in posArray) {
endingPoint.x = image.center.x;
// Do everything else you want to do with the UIImageView inside the array.
}
This inside a loop should work:
endingPoint.x = [[posArray objectAtIndex:i] center].x;
try
endingPoint.x = [[posArray objectAtIndex:i] center].x;
or
endingPoint.x = ((UIImageView *)[posArray objectAtIndex:i]).center.x;
Related
Hi there i have image views in my application to change pictures in them. but their name like imageview1, imageview2, and so.. i create a string which like:
`("imageview%i", number)`
so my string is imageview1 for example. and i need to change
self."**mystring**".image = [uiimage ...]
I looked key-value coding but i couldn't get it exactly. i searched the forum and i can't get anything either. what could i do to resolve this. i think i must do an array with my uiimageviews inside of it. than compare their name with my string (i didn't know how can i get property names as nsstring). then return that image view. Please help.
If you want to do that in runtime, either hack your way using selectors or use tags on your UIImageViews. They are not really good programming concepts, but they work. Examples:
-(void)usingSelectors{
for(int i=0; i < 4; i++){
NSString* propName = [NSString stringWithFormat:#"imageview%d", i];
UIImageView* imageView = [self performSelector:NSSelectorFromString(propName)];
[imageView setImage:/*your image*/];
}
}
-(void)usingTags{
for(int i=0; i < 4; i++){
UIImageView* imageView = [self.view viewWithTag:i];
[imageView setImage:/*your image*/];
}
}
If you want to do that in compile time you'll have to use preprocessor macros, but I don't think that's your case.
I have declared the following CGPoint :
CGPoint borderVertices[5000];
I have added all the values to the array if I may call it (or just a set), but now I was wondering if there is anyway I can NSLog these points or copy them to a file.
I have tried :
NSLog(#"vertices %#", NSStringFromCGPoint(borderVertices));
but I get an error.
What about:
for (NSUInteger i = 0; i < 5000; i++)
{
NSLog(#"vertices :%#", NSStringFromCGPoint(borderVertices[i]));
}
Arrays like in plain old c, needs to be iterated to print each value at it's index.
NSLog(#"vertices %#", NSStringFromCGPoint(borderVertices));
The above statement would have worked if borderVertices is of type CGPoint. But it is not, it is of type CGPoint[].
You could make an array like:
CGPoint borderVertices[5000];
float bVx[5000];
float bVy[5000];
And assign values to bVx and bVy with borderVertices.position.(x or y) in a loop and then whenever you need the coordinates... there you have it.
So I want to use the method appendBezierPathWithPoints:count: in NSBezierPath. But the method requires me to use NSPointArray. The documentary doesn't really talk much about it and all I could get about it is that it's an array of NSPoints and I'm not sure how to do it. I think that it uses the c array mechanism, but I'm not sure.
Thanks.
Yes, you need a C-style array of points to pass to appendBezierPathWithPoints:count:. For example you might do something like this:
NSPoint pointArray[3];
pointArray[0] = NSMakePoint(0, 0);
pointArray[1] = NSMakePoint(0.5, 0.25);
pointArray[2] = NSMakePoint(1, 1);
[lines appendBezierPathWithPoints:pointArray count:3];
where lines is an instance of NSBezierPath.
In a more complicated case you'll use a variable number of points say.
If you want to use Objective-C style array, then you have to use NSValue class for this purpose.
NSMutableArray *array = [NSMutableArray array];
CGPoint myPoint;
myPoint.x = 100;
myPoint.y = 200;
[array addObject:[NSValue valueWithPoint:myPoint]];
To retrieve an NSPoint back from the array:
myPoint = [array[0] pointValue];
Hope it helps.
a big noob needs help understanding things.
I have three UIViews stored inside a NSMutableArray
lanes = [[NSMutableArray arrayWithCapacity:3] retain];
- (void)registerLane:(Lane*)lane {
NSLog (#"registering lane:%i",lane);
[lanes addObject:lane];
}
in the NSLog I see: registering lane:89183264
The value displayed in the NSLog (89183264) is what I am after.
I'd like to be able to save that number in a variable to be able to reuse it elsewhere in the code.
The closest I could come up with was this:
NSString *lane0 = [lanes objectAtIndex:0];
NSString *description0 = [lane0 description];
NSLog (#"description0:%#",description0);
The problem is that description0 gets the whole UIView object, not just the single number (dec 89183264 is hex 0x550d420)
description0's content:
description0:<Lane: 0x550d420; frame = (127 0; 66 460); alpha = 0.5; opaque = NO; autoresize = RM+BM; tag = 2; layer = <CALayer: 0x550d350>>
what I don't get is why I get the correct decimal value with with NSLog so easily, but seem to be unable to get it out of the NSMutableArray any other way. I am sure I am missing some "basic knowledge" here, and I would appreciate if someone could take the time and explain what's going on here so I can finally move on. it's been a long day studying.
why can't I save the 89183264 number easily with something like:
NSInteger * mylane = lane.id;
or
NSInteger * mylane = lane;
thank you all
I'm really confused as to why you want to save the memory location of the view? Because that's what your '89183264' number is. It's the location of the pointer. When you are calling:
NSLog (#"registering lane:%i",lane);
...do you get what's actually being printed out there? What the number that's being printed means?
It seems like a really bad idea, especially when if you're subclassing UIView you've already got a lovely .tag property which you can assign an int of your choosing.
You're making life infinitely more complex than it needs to be. Just use a pointer. Say I have an array containing lots of UIViews:
UIView *viewToCompare = [myArray objectAtIndex:3];
for (id object in myArray) {
if (object == viewToCompare) {
NSLog(#"Found it!");
}
}
That does what you're trying to do - it compares two pointers - and doesn't need any faffing around with ints, etc.
I have been trying to create an array stating the location of a UIImageView in an app I've been working on. What I am trying to do is by using an array I can store the location of my "player" image by using its x,y and z coordinates. The script I am trying to accomplish would look like
NSArray *location[3];
-(IBAction)startup;{
[location addObject: player.center.x];
[location addObject: player.center.y];
[location addObject: playerheight];
}
So I will be able to access this array to move my "player" on the screen in "3-dimensions", but I don't know how to convert the CGpoint values to NSValues so they can be used in the array, is there a simple way to do this inside of the array?
To convert floating point values to objects, use NSNumber. NSValue has wrappers for geometric types like CGPoint. Either would work for you.
[NSValue valueWithCGPoint:player.center];
[NSNumber numberWithFloat:player.center.x];
[NSNumber numberWithFloat:player.center.y];
To addition for the first answer.
When you'll need to read CGPoint back from your array, you can use something like that:
CGPoint point = [(NSValue *)[pointsArray objectAtIndex:i] CGPointValue];
Also note that there's no addObject method for NSArray (you can't add objects to an NSArray after its been created); you want NSMutableArray.
Instead of:
NSArray *location[3];
you probably want something more like:
NSMutableArray *location = [NSMutableArray arrayWithCapacity:3];
Does it have to be an NSArray? Why not use an array of structs?
typedef struct {
CGPoint location;
CGFloat height;
} PlayerLocation;
PlayerLocation players[3];
players[0].location = player.center;
players[0].height = playerheight;
Or depending on your design it may make more sense to declare an objective-C class that contains the x,y,z coordinates as ivars and store those objects into an NSArray.
#interface PlayerLocation : NSObject {
CGPoint location;
CGFloat height;
}
#end