iphone - Mixing NSThreads and NSTimer to update a view -
i want make app make appear , disappear circles on screen. circles enlarged on touchesbegan , smaller on touchesend. i'm able on 1 circle want everywhere user touch screen. know have work nsthreads, example don't work.
this piece of code:
- (void)touchesbegan:(nsset *)touches withevent:(uievent *)event { uitouch *touch = [touches anyobject]; lastpoint = [touch locationinview:self.view]; zoomin = true; rayoncercle = 25; //creation d'un thread timerthread = [[nsthread alloc] initwithtarget:self selector:@selector(cycle) object:nil]; [timerthread start]; if ([touch tapcount] == 1) { nslog(@"une touche"); } if ([touch tapcount] == 2) { drawimage.image = nil; return; } } - (void) cycle { nslog(@"cycle"); nsautoreleasepool* pool = [[nsautoreleasepool alloc] init]; nsrunloop* runloop = [nsrunloop currentrunloop]; time = [nstimer scheduledtimerwithtimeinterval: 0.1 target: self selector: @selector(drawcircle:) userinfo: nil repeats: yes]; [runloop run]; [pool release]; } - (void) drawcircle:(nstimer *)timer { nslog(@"drawcircle"); uigraphicsbeginimagecontext(self.view.frame.size); [drawimage.image drawinrect:cgrectmake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; cggradientref mygradient; cgcolorspaceref mycolorspace; size_t locationcount = 3; cgfloat locationlist[] = {0.0, 0.5, 1.0}; cgfloat colorlist[] = { 1.0, 0.0, 0.5, 1.0, //red, green, blue, alpha 1.0, 0.0, 1.0, 1.0, 0.3, 0.5, 1.0, 1.0 }; mycolorspace = cgcolorspacecreatedevicergb(); mygradient = cggradientcreatewithcolorcomponents(mycolorspace, colorlist, locationlist, locationcount); cgcontextdrawradialgradient(uigraphicsgetcurrentcontext(), mygradient, lastpoint, 0, lastpoint,rayoncercle, 0); drawimage.image = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); if (rayoncercle < 200 && zoomin == true) { _hue += 0.5; _brightness += 0.005; _saturation += 0.05; rayoncercle += 15; } if (zoomin == false) { if (rayoncercle < 0) { [time invalidate]; [timerthread release]; } else { _hue -= 0.5; _brightness -= 0.005; _saturation -= 0.05; rayoncercle -= 1; } } } - (void)touchesmoved:(nsset *)touches withevent:(uievent *)event { uitouch *touch = [touches anyobject]; cgpoint currentpoint = [touch locationinview:self.view]; lastpoint = currentpoint; _hue = 0.0; _saturation = 0.0; _brightness = 0.0; rayoncercle = 25; zoomin = true; } - (void)touchesended:(nsset *)touches withevent:(uievent *)event { _hue = 0.0; _saturation = 0.0; _brightness = 0.0; zoomin = false; uitouch *touch = [touches anyobject]; if ([touch tapcount] == 2) { drawimage.image = nil; return; } }
you don't need secondary thread update view. use delayed callback (nstimer, targeting main thread) invalidate areas must draw, , keep track of position of nearest touch know center drawing. rect invalidation callbacks coalesced using delayed callback, bonus.
Comments
Post a Comment