ios - Do something While view receives gestures -
i want change view backgroundcolor
while receiving uilongpressgesture
, how can properly? code below freezes forever, if lift finger won't unfreeze.
- (void)longpress:(uilongpressgesturerecognizer *)gesture { while ([gesturerecognizer state] == uigesturerecognizerstatechanged) { [self.row2view setbackgroundcolor:[uicolor colorwithwhite:0.5 alpha:0.2]]; } }
edit need is: while uilongpressgesture
change color , when finger lifts screen change color back.
you use while! when enter loop, condition true!
first,change while if. according apple document:
long-press gestures continuous. gesture begins (
uigesturerecognizerstatebegan
) when number of allowable fingers (numberoftouchesrequired
) have been pressed specified period (minimumpressduration
) , touches not move beyond allowable range of movement (allowablemovement
). gesture recognizer transitions change state whenever finger moves, , ends (uigesturerecognizerstateended
) when of fingers lifted.
i think should use began state(when gesture got recognized) or ended state(when recognized gesture release up). if don't want call method continuously when touch moves.
if ([gesture state] == uigesturerecognizerstatebegan) { [self.row2view setbackgroundcolor:[uicolor colorwithwhite:0.5 alpha:0.2]]; }
--- edit ---
according description, want change color when touch , restore when release. be:
if ([gesture state] == uigesturerecognizerstatebegan) { row2vieworigincolor = self.row2view.backgroundcolor; // can declare var in class. [self.row2view setbackgroundcolor:[uicolor colorwithwhite:0.5 alpha:0.2]]; } else if ([gesture state] == uigesturerecognizerstateended || [gesture state] == uigesturerecognizerstatecancelled) { [self.row2view setbackgroundcolor: row2vieworigincolor]; }
Comments
Post a Comment