A uilabel that triggers a click event and changes color
Who said that uilabel could not process click events as a button? Today, I want to provide a modified uilabel that can trigger click events and change color :)
:
You can also use timer as a Timer:
The source code is as follows:
Taplabel. h and taplabel. m
//// Taplabel. h // taplabel // copyright (c) 2014 y. x. all rights reserved. // # import <uikit/uikit. h> @ Class taplabel; @ protocol taplabeldelegate <nsobject>-(void) taplabelevent :( taplabel *) label; @ end @ interface taplabel: uilabel @ property (nonatomic, assign) ID <taplabeldelegate> delegate; // Protocol @ property (nonatomic, strong) nsstring * icationicationname; // set the name of the notification center @ property (nonatomic, strong) nsdictionary * metadata; // metadata @ end
//// Taplabel. M // taplabel // copyright (c) 2014 y. x. all rights reserved. // # import "taplabel. H "@ implementation taplabel-(ID) initwithframe :( cgrect) frame {self = [Super initwithframe: frame]; If (Self) {self. userinteractionenabled = yes; uilongpressgesturerecognizer * tap = [[uilongpressgesturerecognizer alloc] initwithtarget: Self action: @ selector (labelevent :)]; tap. minimumpressduration = 0.01f; [self addgesturerecognizer: tap];} return self;}-(void) labelevent :( uilongpressgesturerecognizer *) gesture {// obtain the coordinate value cgpoint locationpoint = [gesture locationinview: self]; // status 1 If (gesture. state = uigesturerecognizerstatebegan) {self. highlighted = yes;} // status 2 if (gesture. state = uigesturerecognizerstatechanged) {If (locationpoint. x <= self. bounds. size. width & locationpoint. x> = 0 & locationpoint. Y <= self. bounds. size. height & locationpoint. y> = 0) {self. highlighted = yes;} else {self. highlighted = no; }}// status 3 if (gesture. state = uigesturerecognizerstateended) {If (locationpoint. x <= self. bounds. size. width & locationpoint. x> = 0 & locationpoint. Y <= self. bounds. size. height & locationpoint. y> = 0) {If (_ delegate) {[_ delegate taplabelevent: Self];} If (_ icationicationname) {[[nsicationicationcenter defacenter center] postnotificationname: _ icationicationname object: nil userinfo: @ {@ "taplabel": Self}] ;}} self. highlighted = no; }}@ end
Source code for use:
//// Rootviewcontroller. M // taplabel // copyright (c) 2014 y. x. all rights reserved. // # import "rootviewcontroller. H "# import" taplabel. H "@ interface rootviewcontroller () <taplabeldelegate> @ end @ implementation rootviewcontroller-(void) viewdidload {[Super viewdidload]; taplabel * tap = [[taplabel alloc] initwithframe: cgrectmake (0, 0,320, 20)]; tap. textalignment = nstextalignmentcenter; tap. center = self. view. center; tap. TEXT = @ "youxianming"; tap. font = [uifont fontwithname: @ "helveticaneue-thin" Size: 18]; tap. delegate = self; tap. metadata =@{@ "name": @ "youxianming"}; tap. highlightedtextcolor = [uicolor redcolor]; [self. view addsubview: tap];}-(void) taplabelevent :( taplabel *) label {nslog (@ "% @", label. metadata);} @ end
Principle Analysis:
1. Added gesture processing after initialization:
2. accurately calculate the three statuses of gestures
3. uilabel comes with highlightedtextcolor :)
The principle is so simple :)
A uilabel that triggers a click event and changes color