ios - Swapping images in an IBOutletCollection -
i having trouble correctly swapping 2 uiimageviews stored in iboutletcollection. conceptually, must doing wrong.
let's have nsmutablearray of indexed data, , nsmutablearray of indexed uiimageviews, 2 indexed arrays correspond, i.e. nth-indexed element of uiimageview array should reflect nth data element in image array.
@property (nonatomic, strong) iboutletcollection(myimageview) nsmutablearray* myimages; @property (nonatomic, strong) nsmutablearray* mydata;
at outset, sort iboutletcollection x-coordinate appearance on screen left-to-right, i.e. element of index 0 should appear way left, ..., way right of screen.
nscomparisonresult imagesort(id label1, id label2, void* context) { if ([label1 frame].origin.x < [label2 frame].origin.x) return nsorderedascending; else if ([label1 frame].origin.x > [label2 frame].origin.x) return nsordereddescending; else { // determine using y-coordinate if ([label1 frame].origin.y < [label2 frame].origin.y) return nsorderedascending; else if ([label1 frame].origin.y > [label2 frame].origin.y) return nsordereddescending; else return nsorderedsame; } }
now, whenever want swap 2 members of data array, make sure swap images well, each uiimageview reflect data in slot. let's 2 elements want swap have indices frontindex , backindex:
// switch state data in arrays data* sendtoback = mydata[frontindex]; data* bringtofront = mydata[backindex]; mydata[frontindex] = bringtofront; mydata[backindex] = sendtoback; myimageview* sendtobackimg = myimages[frontindex]; myimageview* bringtofrontimg = myimages[backindex]; myimages[frontindex] = bringtofrontimg; myimages[backindex] = sendtobackimg;
the problem happens when try animate or update image array. appears when call animate or update on updated array element @ index 0 , 9, views update aren't ones located leftmost , 9th left: they're updating in new locations:
[myimages[frontindex] animateinwayx]; --> updates on-screen view @ backindex [myimages[backindex] animateinwayy]; --> updates on-screen view @ frontindex
i checked arrays in debugger, , swap did happen -- in other words, frontindex element inside myimages array show proper data reflecting model @ mydata[frontindex], view array swapped, it's displaying @ new location on screen (the location of backindex, if didn't move).
how fix this?
yes, commenter above pointed out, swapping pointers only. best solution copy contents being pointed-to.
// switch state data in arrays data* temp = [[dataobject alloc] init]; [self copydatafrom:bringtofront into:temp]; [self copydatafrom:sendtoback into:mydata[frontindex]]; [self copydatafrom:temp into:mydata[backindex]]; myimageview* frontimg = myimages[frontindex]; myimageview* backimg = myimages[backindex]; [frontimg updateusingdata:mydata[frontindex]]; [backimg updateusingdata:mydata[backindex]];
Comments
Post a Comment