objective c - GCD Scrollview loading issue -
i'm using gcd load images scroll view rather following frames created makes single frame in images flicker through. here's code:
//create array -(void)awakefromnib { images = [nsmutablearray arraywithobjects: [uiimage imagewithcontentsoffile:[[nsbundle mainbundle] pathforresource:@"imageone" oftype:@"png"]], [uiimage imagewithcontentsoffile:[[nsbundle mainbundle] pathforresource:@"imagetwo" oftype:@"png"]], [uiimage imagewithcontentsoffile:[[nsbundle mainbundle] pathforresource:@"imagethree" oftype:@"png"]],nil]; } //populate scrollview -(void)populate { dispatch_queue_t queue = dispatch_get_global_queue(dispatch_queue_priority_high, 0ul); scrollview.pagingenabled = yes; scrollview.backgroundcolor = [uicolor clearcolor]; scrollview.clipstobounds = yes; scrollview.delegate = self; [scrollview setcontentsize:cgsizemake(2 * scrollview.frame.size.width, scrollview.frame.size.height)]; int number = [images count] ; nslog(@"%i",images); int rowtwoint = 0; int rowthreeint = 0; int rowfourint = 0; (int = 0; < number; i++) { if (i <= 3) { finalframe = cgrectmake( i*(80), 12, 80, 80); } if (i == 4) { finalframe = cgrectmake(0, 95, 80, 80); } if ((i >4) && (i <= 7)) { rowtwoint = rowtwoint + 80; finalframe = cgrectmake(rowtwoint, 95, 80, 80); } if (i == 8) { finalframe = cgrectmake(0, 178, 80, 80); } if ((i > 8) && (i <= 11)) { rowthreeint = rowthreeint + 80; finalframe = cgrectmake(rowthreeint, 178, 80, 80); } if (i == 12) { finalframe = cgrectmake(0, 261, 80, 80); } if ((i > 12) && (i <= 15)) { rowfourint = rowfourint + 80; finalframe = cgrectmake(rowfourint, 261, 80, 80); } iconbutton = [uibutton buttonwithtype:uibuttontypecustom]; iconbutton.frame = cgrectmake(0, 0, 57, 57); iconbutton.center = cgpointmake(40, 29); [iconbutton addtarget:self action:@selector(selected:) forcontrolevents:uicontroleventtouchupinside]; iconbutton.tag = i; dispatch_async(queue, ^{ uiimage *icon = [images objectatindex:i]; dispatch_sync(dispatch_get_main_queue(), ^{ [iconbutton setimage:icon forstate:uicontrolstatenormal]; }); }); [cell addsubview:iconbutton]; cell = [[uiview alloc] initwithframe:finalframe]; [scrollview addsubview:cell];
so doing wrong? i'm new gcd it's that. if see way improve uiscrollview performance, please let me know.
i suspect have declared iconbutton
instance variable, static
variable, or global variable.
each time through loop, change value of iconbutton
. time twelve queue blocks execute, iconbutton
set final value - reference last button created. blocks use instance (or static
or global) iconbutton
variable, blocks update same, final button.
make local variable reference button, , use local variable in loop:
uibutton *button = [uibutton buttonwithtype:uibuttontypecustom]; iconbutton = button; iconbutton.frame = cgrectmake(0, 0, 57, 57); ... dispatch_async(queue, ^{ uiimage *icon = [images objectatindex:i]; dispatch_sync(dispatch_get_main_queue(), ^{ [button setimage:icon forstate:uicontrolstatenormal]; }); });
each block capture value of local variable @ moment block created, each block capture reference different button.
Comments
Post a Comment