I have an array of UIImageViews:
NSMutableArray *imagesArray = [[NSMutableArray alloc] init];
[imagesArray addObject:smallBox];
[imagesArray addObject:medBox];
[imagesArray addObject:largeBox];
[imagesArray addObject:xlBox];
These boxes are being moved on the screen. I would like the array sorted so that whichever box is on the left side of the screen (x position) is in the first position of the array etc. I think I would be able to figure it out if I knew how to access the properties of the images while in the array. I tried using code like imagesArray[0].frame.origin.x; but that does not work. Any help would be appreciated.
I’d do it like this:
That’s assuming that all the objects in the array are views — make sure that
smallBox,medBox, etc. are all instances ofUIImageViewand notUIImage. Also, note that in order for the image views’xcoordinate to be sort the way you want them to, they should all be subviews of the same superview. If they’re not, you’re not comparing apples to apples. If the views are not all subviews of the same view, you’ll need to write a comparator that first converts the view’sframeto screen coordinates before comparing.Your expression (
imagesArray[0].frame.origin.x) should give you thexcoordinate of the view atimagesArray[0], and if it doesn’t it’d be helpful to know how/why it’s failing.