Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 17th, 2012  |  syntax: None  |  size: 3.68 KB  |  hits: 11  |  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. change button background color when clicked
  2. button1.layer.borderColor = [[UIColor purpleColor]CGColor];
  3.     button1.layer.cornerRadius = 8.0f;
  4.     button1.layer.masksToBounds = YES;
  5.     button1.layer.borderWidth = 1.0f;
  6.     [button1.tintColor = [UIColor redColor]CGColor];
  7.     [button1 setTintColor:[UIColor blueColor]];
  8.     [button1 setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal];
  9.     [button1 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
  10.     [button1 setTintColor:[UIColor redColor]];
  11.        
  12. [button1 setBackgroundImage:[UIImage imageNamed:@"redButton.png"]];
  13.        
  14. [button1 setBackgroundColor:[UIColor redColor] ];
  15.        
  16. CALayer * layer = [button1 layer];    
  17.      [layer setCornerRadius:8.0f];
  18.      [layer setMasksToBounds:YES];
  19.      [layer setBorderWidth:1.0f];
  20.      [layer setBorderColor:[[UIColor whiteColor] CGColor]];
  21.      button1.backgroundColor = [UIColor redColor];
  22.        
  23. @interface MyButton : UIButton
  24. {
  25. @private
  26.     NSMutableDictionary *backgroundStates;
  27. @public
  28.  
  29. }
  30.  
  31. - (void) setBackgroundColor:(UIColor *) _backgroundColor forState:(UIControlState) _state;
  32. - (UIColor*) backgroundColorForState:(UIControlState) _state;
  33.  
  34. @end
  35.        
  36. #import "MyButton.h"
  37.  
  38.  
  39. @implementation MyButton
  40.  
  41. - (void) setBackgroundColor:(UIColor *) _backgroundColor forState:(UIControlState) _state {
  42.     if (backgroundStates == nil)
  43.         backgroundStates = [[NSMutableDictionary alloc] init];
  44.  
  45.     [backgroundStates setObject:_backgroundColor forKey:[NSNumber numberWithInt:_state]];
  46.  
  47.     if (self.backgroundColor == nil)
  48.         [self setBackgroundColor:_backgroundColor];
  49. }
  50.  
  51. - (UIColor*) backgroundColorForState:(UIControlState) _state {
  52.     return [backgroundStates objectForKey:[NSNumber numberWithInt:_state]];
  53. }
  54.  
  55. #pragma mark -
  56. #pragma mark Touches
  57.  
  58. - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  59.     [super touchesBegan:touches withEvent:event];
  60.  
  61.     UIColor *selectedColor = [backgroundStates objectForKey:[NSNumber numberWithInt:UIControlStateHighlighted]];
  62.     if (selectedColor) {
  63.         CATransition *animation = [CATransition animation];
  64.         [animation setType:kCATransitionFade];
  65.         [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
  66.         [self.layer addAnimation:animation forKey:@"EaseOut"];
  67.         self.backgroundColor = selectedColor;
  68.     }
  69. }
  70.  
  71. - (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
  72.     [super touchesCancelled:touches withEvent:event];
  73.  
  74.     UIColor *normalColor = [backgroundStates objectForKey:[NSNumber numberWithInt:UIControlStateNormal]];
  75.     if (normalColor) {
  76.         CATransition *animation = [CATransition animation];
  77.         [animation setType:kCATransitionFade];
  78.         [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
  79.         [self.layer addAnimation:animation forKey:@"EaseOut"];
  80.         self.backgroundColor = normalColor;
  81.     }
  82. }
  83.  
  84. - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  85.     [super touchesEnded:touches withEvent:event];
  86.  
  87.     UIColor *normalColor = [backgroundStates objectForKey:[NSNumber numberWithInt:UIControlStateNormal]];
  88.     if (normalColor) {
  89.         CATransition *animation = [CATransition animation];
  90.         [animation setType:kCATransitionFade];
  91.         [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
  92.         [self.layer addAnimation:animation forKey:@"EaseOut"];
  93.         self.backgroundColor = normalColor;
  94.     }
  95. }
  96.  
  97. - (void) dealloc {
  98.     [backgroundStates release];
  99.     [super dealloc];
  100. }
  101.  
  102. @end
  103.        
  104. [button1 setBackgroundColor:[UIColor colorWithRed:0.8 green:0.7 blue:0.6 alpha:1.0] forState:UIControlStateHighlighted];