ios - How to remove gradient effect for UIButton after setting once - iPhone -


i using uibutton soft key items. when click show color effects button. used below code it.

[btn settitlecolor:[uicolor lightgraycolor] forstate:uicontrolstatehighlighted];      cagradientlayer *btngradient = [cagradientlayer layer];     btngradient.frame = btn.bounds;     btngradient.colors = [nsarray arraywithobjects:                           (id)[[uicolor colorwithred:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:.6f] cgcolor],                           (id)[[uicolor colorwithred:200.0f/255.0f green:200.0f/255.0f blue:200.0f/255.0f alpha:.4f] cgcolor],                           (id)[[uicolor colorwithred:150.0f/255.0f green:150.0f/255.0f blue:150.0f/255.0f alpha:.4f] cgcolor],                           (id)[[uicolor colorwithred:100.0f/255.0f green:100.0f/255.0f blue:100.0f/255.0f alpha:.4f] cgcolor],                           (id)[[uicolor colorwithred:50.0f/255.0f green:50.0f/255.0f blue:50.0f/255.0f alpha:.4f] cgcolor],                           (id)[[uicolor colorwithred:5.0f/255.0f green:5.0f/255.0f blue:5.0f/255.0f alpha:.4f] cgcolor],                            nil];     [btn.layer insertsublayer:btngradient atindex:0];      cagradientlayer *glosslayer = [cagradientlayer layer];     glosslayer.frame = btn.bounds;     glosslayer.colors = [nsarray arraywithobjects:                          (id)[uicolor colorwithwhite:1.0f alpha:0.4f].cgcolor,                          (id)[uicolor colorwithwhite:1.0f alpha:0.1f].cgcolor,                          (id)[uicolor colorwithwhite:0.75f alpha:0.0f].cgcolor,                          (id)[uicolor colorwithwhite:1.0f alpha:0.1f].cgcolor,                          nil];     glosslayer.locations = [nsarray arraywithobjects:                             [nsnumber numberwithfloat:0.0f],                             [nsnumber numberwithfloat:0.5f],                             [nsnumber numberwithfloat:0.5f],                             [nsnumber numberwithfloat:1.0f],                             nil];     [btn.layer insertsublayer:glosslayer atindex:0];      calayer *btnlayer = [btn layer];     [btnlayer setmaskstobounds:yes];      uicolor*mycolor = btn.backgroundcolor;     [btn.layer setbordercolor:[mycolor cgcolor]];   

trying remove gradient effects button in below way. making alpha zero.. didnt work..

-(void) removegradient:(uibutton *)btn{      cagradientlayer *btngradient = [cagradientlayer layer];     btngradient.frame = btn.bounds;     btngradient.colors = [nsarray arraywithobjects:                           (id)[[uicolor colorwithred:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:.0f] cgcolor],                           (id)[[uicolor colorwithred:200.0f/255.0f green:200.0f/255.0f blue:200.0f/255.0f alpha:.0f] cgcolor],                           (id)[[uicolor colorwithred:150.0f/255.0f green:150.0f/255.0f blue:150.0f/255.0f alpha:.0f] cgcolor],                           (id)[[uicolor colorwithred:100.0f/255.0f green:100.0f/255.0f blue:100.0f/255.0f alpha:.0f] cgcolor],                           (id)[[uicolor colorwithred:50.0f/255.0f green:50.0f/255.0f blue:50.0f/255.0f alpha:.0f] cgcolor],                           (id)[[uicolor colorwithred:5.0f/255.0f green:5.0f/255.0f blue:5.0f/255.0f alpha:.0f] cgcolor],                             nil];     [btn.layer insertsublayer:btngradient atindex:0];      cagradientlayer *glosslayer = [cagradientlayer layer];     glosslayer.frame = btn.bounds;     glosslayer.colors = [nsarray arraywithobjects:                          (id)[uicolor colorwithwhite:1.0f alpha:0.0f].cgcolor,                          (id)[uicolor colorwithwhite:1.0f alpha:0.0f].cgcolor,                          (id)[uicolor colorwithwhite:0.75f alpha:0.0f].cgcolor,                          (id)[uicolor colorwithwhite:1.0f alpha:0.0f].cgcolor,                          nil];     glosslayer.locations = [nsarray arraywithobjects:                             [nsnumber numberwithfloat:0.0f],                             [nsnumber numberwithfloat:0.5f],                             [nsnumber numberwithfloat:0.5f],                             [nsnumber numberwithfloat:1.0f],                             nil];     //[btn.layer insertsublayer:glosslayer atindex:0];      calayer *btnlayer = [btn layer];     [btnlayer setmaskstobounds:yes];  } 

here, if click once working fine. if click on button repeatedly, color reapplied button , after around 10 clicks button turns in white color.

i think have remove prior gradient effect layer if have redo effect. how remove gradient effect on layer after setting once.

thanks jithen

what asking common , can achieved through subclassing uibutton. after have created subclass add uiresponder touchevents subclass. in uibutton subclass, setup ivar gradient , in touches began , touches ended, change the properties of gradient. on touch began can invert , on touches ended can set original gradient.

  1. subclass uibutton
  2. create ivar cagradient
  3. setup gradient in initwithframe method of uibutton subclass
  4. add uiresponder touch methods
  5. in touchesbegan - modify gradient desire.
  6. repeat step 5 in touchesended

again suggest subclassing because need capture touch events button. cannot in main view controller have setup button in.

let me know if helps.

edit #1 - take @ example below:

-(void) touchesbegan:(nsset *)touches withevent:(uievent *)event{     gradient.colors = [nsarray arraywithobjects:(id)[uicolor colorwithwhite:0.0f alpha:0.25f].cgcolor,                                                 (id)[uicolor colorwithwhite:0.0f alpha:0.25f].cgcolor,                                                 nil];     gradient.locations = [nsarray arraywithobjects: [nsnumber numberwithfloat:0.0f],                                                     [nsnumber numberwithfloat:0.75f],                                                     nil]; }  -(void)touchesended:(nsset *)touches withevent:(uievent *)event{     gradient.colors = [nsarray arraywithobjects:(id)[uicolor colorwithwhite:1.0f alpha:0.25f].cgcolor,                                                 (id)[uicolor colorwithwhite:0.0f alpha:0.25f].cgcolor,                                                 nil];     gradient.locations = [nsarray arraywithobjects: [nsnumber numberwithfloat:0.0f],                                                     [nsnumber numberwithfloat:0.75f],                                                     nil]; } 

if notice modifying existing gradient. not creating new 1 each time. values have not work concept of not creating new gradient each tap. modifying gradient property only.

edit #2 - create ivar gradients:

  1. go the uibutton subclass header
  2. add under interface section:

    cagradientlayer *btngradient;

    cagradientlayer *glosslayer;


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -