iOS Click to change uiview background color is a more common requirement. First reaction should be no trouble, so wrote a first version of
@interface Respondentuiview (){ * bgColor;} @end @implementation Respondentuiview
-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *)event { = Self.backgroundcolor; =-(void) touchesended: (Nsset<uitouch *> *) touches withevent: (uievent *)event { = bgColor;}
@end
It seems to work. But there is a problem. Found that Touchesbegan is very time-delayed very serious appearance. Then there is the second version.
@interfaceRespondentuiview () {Uicolor*BgColor;}@end@implementationRespondentuiview- (void) Willmovetosuperview: (UIView *) newsuperview{Uilongpressgesturerecognizer*taprecognizer =[[Uilongpressgesturerecognizer alloc] initwithtarget:self action: @selector (ChangeColor:)]; Taprecognizer.minimumpressduration=0;//Up-to-you ;Taprecognizer.cancelstouchesinview =NO; [Self Addgesturerecognizer:taprecognizer];}-(void) ChangeColor: (Uigesturerecognizer *) gesturerecognizer{if(Gesturerecognizer.state = =Uigesturerecognizerstatebegan) {BgColor=Self.backgroundcolor; Self.backgroundcolor=White_down; } Else if(Gesturerecognizer.state = =uigesturerecognizerstateended) {Self.backgroundcolor=BgColor; }}@end
With Uilongpressgesturerecognizer suddenly is much better, click Reaction Swish. One can find the problem again, some view need to register Click event, a uiview register multiple Uigesturerecognizer, there is always one not responding. It's shit. So again Google a pass, found a UIView use multiple
Uigesturerecognizer method, so there is a third edition.
@interfaceRespondentuiview () <UIGestureRecognizerDelegate>{Uicolor*BgColor;}@end@implementationRespondentuiview- (void) Willmovetosuperview: (UIView *) newsuperview{Uilongpressgesturerecognizer*taprecognizer =[[Uilongpressgesturerecognizer alloc] initwithtarget:self action: @selector (ChangeColor:)]; Taprecognizer.minimumpressduration=0;//Up-to-you ;Taprecognizer.cancelstouchesinview =NO; Taprecognizer.Delegate=Self ; [Self Addgesturerecognizer:taprecognizer];}-(void) ChangeColor: (Uigesturerecognizer *) gesturerecognizer{if(Gesturerecognizer.state = =Uigesturerecognizerstatebegan) {BgColor=Self.backgroundcolor; Self.backgroundcolor=White_down; } Else if(Gesturerecognizer.state = =uigesturerecognizerstateended) {Self.backgroundcolor=BgColor; }}//The following three functions are used by multiple gesturerecognizer to work together to avoid pressing gestures and other gestures do not respond-(BOOL) Gesturerecognizershouldbegin: (Uigesturerecognizer *) gesturerecognizer{returnYES;}-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) othergesturerecognizer{returnYES;}-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldreceivetouch: (Uitouch *) touch{returnYES;}@end
It feels like the whole world is quiet. Incidentally, please.
Taprecognizer.cancelstouchesinview = NO;
This is a very sharp attribute. The general iOS response chain is delivered in the order given to UIView's uigesturerecognizer processing first. If it is handled, the event is discarded, so the uiview above the Touchesbegan and touchesended priority than the lower Uiresponder will not be able to respond. Set the Cancelstouchesinview to No. So everyone can get along harmoniously.
iOS Click to change uiview background color