ios - How to change border of UIButton if touched outside of UIButton? -


currently trying create stereotypical "selected" action of object. is, when click on object, border changes different color, , when click outside object, border changes normal color. can figure out how change border of object when touch inside of object(in case, uibutton) however, cannot figure out how change border of uibutton original state when touch outside of uibutton. here code far:

- (void)longpress:(uilongpressgesturerecognizer*)gesture { if ( gesture.state == uigesturerecognizerstatebegan ) {     gesture.view.layer.bordercolor = [uicolor lightgraycolor].cgcolor;       uialertcontroller * alert=   [uialertcontroller                                   alertcontrollerwithtitle:@"would delete rep?"                                   message:nil                                   preferredstyle:uialertcontrollerstylealert];      uialertaction* deletebutton = [uialertaction                                 actionwithtitle:@"delete"                                 style:uialertactionstyledefault                                 handler:^(uialertaction * action)                                 {                                            [gesture.view removefromsuperview];                                      [alert dismissviewcontrolleranimated:yes completion:nil];                                  }];     uialertaction* cancelbutton = [uialertaction                                actionwithtitle:@"cancel"                                style:uialertactionstyledefault                                handler:^(uialertaction * action)                                    {                                         gesture.view.layer.bordercolor = [uicolor blackcolor].cgcolor;                                     [alert dismissviewcontrolleranimated:yes completion:nil];                                 }];       [alert addaction:deletebutton];      [alert addaction:cancelbutton];       [self presentviewcontroller:alert animated:yes completion:nil];      }      }     - (void)panwasrecognized:(uipangesturerecognizer *)panner {   {      panner.view.layer.bordercolor = [uicolor lightgraycolor].cgcolor;      _draggedview = panner.view;      cgpoint offset = [panner translationinview:_draggedview.superview];     cgpoint center = _draggedview.center;     _draggedview.center = cgpointmake(center.x + offset.x, center.y + offset.y);     _buttonfield.layer.borderwidth = 4.0f;         // reset translation 0 on next `panwasrecognized:` message,     // translation additional movement of touch since now.     [panner settranslation:cgpointzero inview:_draggedview.superview];   }   }    -(void)buttontouched:(uibutton*)sender forevent:(id)tap { nsset *touches = [tap alltouches]; uitouch *touch = [touches anyobject]; uitouchphase *phase = touch.phase; touch.view.layer.bordercolor = [uicolor lightgraycolor].cgcolor; }   -(void)doubletapped:(uibutton*)sender forevent:(id)twotaps { nsset *touches = [twotaps alltouches]; uitouch *touch = [touches anyobject]; uitouchphase *phase = touch.phase; touch.view.layer.bordercolor = [uicolor blackcolor].cgcolor;  }     - (ibaction)addrepbutton:(uibarbuttonitem *)newrep {  self.labelcounter++;  buttoncount ++; if (buttoncount > 0 ) {      _buttonfield = [[uibutton alloc]initwithframe:cgrectmake(300, 300, 28, 28)];     [_buttonfield settitle:[nsstring stringwithformat:@"%i", self.labelcounter]forstate:uicontrolstatenormal];     [_buttonfield settitlecolor:[uicolor blackcolor] forstate:uicontrolstatenormal];     _buttonfield.contenthorizontalalignment = uicontrolcontenthorizontalalignmentcenter;     _buttonfield.userinteractionenabled = yes;     _buttonfield.layer.cornerradius = 14;     _buttonfield.layer.bordercolor = [uicolor blackcolor].cgcolor;     _buttonfield.layer.borderwidth = 4.0f;     _buttonfield.titlelabel.font = [uifont systemfontofsize: 18];     [_buttonfield settitlecolor:[uicolor whitecolor] forstate:uicontrolstatenormal];     _buttonfield.layer.backgroundcolor = [uicolor blackcolor].cgcolor;        //pan gesture declared in button     uipangesturerecognizer *panner = [[uipangesturerecognizer alloc] initwithtarget:self action:@selector(panwasrecognized:)];     [_buttonfield addgesturerecognizer:panner];      //long press gesture declared in button     uilongpressgesturerecognizer *longpress = [[uilongpressgesturerecognizer alloc] initwithtarget:self action:@selector(longpress:)];     [self.buttonfield addgesturerecognizer:longpress];      //touch down inside declared in button     [self.buttonfield addtarget:self action:@selector(buttontouched:forevent:) forcontrolevents:uicontroleventtouchdown];      //double tap inside declared in button     [self.buttonfield addtarget:self action:@selector(doubletapped:forevent:) forcontrolevents:uicontroleventtouchdownrepeat];          [self.view addsubview:_buttonfield];       }       }            @end 

i need find out how change border of uibutton normal when touching outside of uibutton in order true "select/deselect" feel.

it seems problem isn't can't change border as can't trigger action when user touches outside of button.

i suggest approaching problem perspective of setting button unselected anytime user not touch button. there couple ways that.

one option, if of other buttons , touch areas disable button, add action deselect button when other button touched.

uisegmentedcontroller designed allow 1 item selected time option if design allows it.

you @ touch events on screen , touch state uigesturerecognizerstatebegan outside of button set button unselected. this:

- (void)touchesbegan:(nsset<uitouch *> *)touches withevent:(nullable uievent *)event {     uitouch *atouch = [touches anyobject];     cgpoint point = [atouch locationinview:self.mybutton.superview];     if (!cgrectcontainspoint(self.mybutton, point)) {         // deselect button     } } 

there more ways try solve problem, find these 3 cover situation.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -