iOS Click to change uiview background color

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.