Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

gradientButton

By: a guest on Sep 6th, 2012  |  syntax: Objective C  |  size: 2.20 KB  |  hits: 50  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. #import "gradientButton.h"
  3. @interface gradientButton()
  4. @property (nonatomic, strong) CAGradientLayer* gradientLayer;
  5. @property (nonatomic, strong) CAGradientLayer* glossyLayer;
  6. @end
  7. @implementation gradientButton
  8.  
  9.  
  10. -(void)awakeFromNib
  11. {
  12.     _gradientLayer = [[CAGradientLayer alloc] init];
  13.     _gradientLayer.bounds = self.bounds;
  14.     _gradientLayer.position = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
  15.     [self.layer insertSublayer:_gradientLayer atIndex:0];
  16.    
  17.         self.layer.cornerRadius = 5.0f;
  18.         self.layer.masksToBounds = YES;
  19.         self.layer.borderWidth = 1.0f;
  20.     _glossyLayer = [[CAGradientLayer alloc] init];
  21.         _glossyLayer.bounds = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height/2);
  22.     _glossyLayer.position = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/4);
  23.     [self.layer addSublayer:_glossyLayer];
  24.    
  25.     [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  26.     [self setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];
  27.  
  28. }
  29. #define GRADIENT_TOP [UIColor colorWithRed:38.0/255.0 green:78.0/255.0 blue:54.0/255.0 alpha:1]
  30. #define GRADIENT_BOTTOM [UIColor colorWithRed:44.0/255.0 green:71.0/255.0 blue:56.0/255.0 alpha:1]
  31. #define GLOSS_TOP [UIColor colorWithRed:0.70f green:0.70f blue:0.70f alpha:0.95f]
  32. #define GLOSS_BOTTOM [UIColor colorWithRed:0.70f green:0.70f blue:0.70f alpha:0.35f]
  33. #define GRADIENT_SELECTED_TOP [UIColor colorWithRed:138.0/255.0 green:178.0/255.0 blue:154.0/255.0 alpha:1]
  34. #define GRADIENT_SELECTED_BOTTOM [UIColor colorWithRed:114.0/255.0 green:171.0/255.0 blue:156.0/255.0 alpha:1]
  35.  
  36. -(void)setHighlighted:(BOOL)highlighted{
  37.     [super setHighlighted:highlighted];
  38.     if(highlighted)
  39.         [_gradientLayer setColors:@[(id)[GRADIENT_SELECTED_TOP CGColor],(id)[GRADIENT_SELECTED_BOTTOM CGColor]]];
  40.     else
  41.         [_gradientLayer setColors:@[(id)[GRADIENT_TOP CGColor],(id)[GRADIENT_BOTTOM CGColor]]];
  42. }
  43.  
  44. - (void)drawRect:(CGRect)rect
  45. {
  46.    
  47.     [_gradientLayer setColors:@[(id)[GRADIENT_TOP CGColor],(id)[GRADIENT_BOTTOM CGColor]]];
  48.    
  49.     [_glossyLayer setColors:@[(id)[GLOSS_TOP CGColor], (id)[GLOSS_BOTTOM CGColor]]];
  50.     [super drawRect:rect];
  51. }
  52.  
  53.  
  54.  
  55. @end