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

Untitled

By: a guest on Apr 27th, 2012  |  syntax: None  |  size: 4.99 KB  |  hits: 86  |  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. How to fire hightlight effect on UIBarButtonItem
  2. - (void)highlightButton {
  3.     NSArray *originalFooterItems = self.toolbarItems;
  4.     NSMutableArray *footerItems = [originalFooterItems mutableCopy];
  5.     [footerItems removeLastObject];
  6.  
  7.     NSString* pathToImageFile = [[NSBundle mainBundle] pathForResource:@"white_glow" ofType:@"png"];
  8.     UIImage* anImage = [UIImage imageWithContentsOfFile:pathToImageFile];
  9.     UIImageView *imageView = [[UIImageView alloc] initWithImage:anImage];
  10.     imageView.frame = CGRectMake(0, 0, 40, 40);
  11.  
  12.     UIBarButtonItem *flashImage = [[UIBarButtonItem alloc] initWithCustomView:imageView];
  13.     [footerItems addObject:flashImage];
  14.  
  15.     [UIView animateWithDuration:0.3
  16.                           delay: 0.0
  17.                         options: UIViewAnimationOptionCurveEaseOut
  18.                      animations:^{
  19.                          self.toolbarItems = footerItems;                     }
  20.                      completion:^(BOOL finished){
  21.                          [UIView animateWithDuration:.3
  22.                                                delay: 0.0
  23.                                              options:UIViewAnimationOptionCurveEaseIn
  24.                                           animations:^{
  25.                                               self.toolbarItems = originalFooterItems;
  26.                                           }
  27.                                           completion:nil];
  28.                      }];
  29. }
  30.        
  31. [(UIButton *)[[toolbarItems objectAtIndex:1] customView] setImage:[UIImage imageNamed:@"highlight.png"] forState:UIControlStateNormal];
  32.        
  33. UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  34. [button addTarget:self
  35.            action:@selector(someFunction:)
  36.  forControlEvents:UIControlEventTouchDown];
  37. [button setTitle:@"Click here" forState:UIControlStateNormal];
  38. button.frame = CGRectMake(0.0, 0.0, 100.0, 40.0);
  39. [self.view addSubview:button];
  40.        
  41. [button setTitle:@"Look Here" forState:UIControlStateNormal];
  42.        
  43. btnImage = [UIImage imageNamed:@"highlight.png"];
  44. [button setImage:btnImage forState:UIControlStateNormal];
  45.        
  46. - (void)highlightButton:(UIButton *)button {
  47.     [button setHighlighted:YES];
  48. }
  49.        
  50. - (void)viewDidLoad
  51. {
  52.     [super viewDidLoad];
  53.     UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"Custom"
  54.                                                                  style:UIBarButtonItemStylePlain
  55.                                                                 target:self
  56.                                                                 action:nil];
  57.     self.navigationItem.rightBarButtonItem = rightBtn;
  58.     [rightBtn release];
  59.  
  60.     [self performSelector:@selector(highlight)
  61.                withObject:nil
  62.                afterDelay:2.0];
  63. }
  64.        
  65. - (void) highlight{
  66.     UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"Custom"
  67.                                                                  style:UIBarButtonItemStyleDone
  68.                                                                 target:self
  69.                                                                 action:nil];
  70.     self.navigationItem.rightBarButtonItem = rightBtn;
  71.     [rightBtn release];
  72. }
  73.        
  74. UIButton *favButton = [UIButton buttonWithType:UIButtonTypeCustom];
  75. [favButton setFrame:CGRectMake(0.0, 100.0, 50.0, 30.0)];
  76. [favButton setBackgroundImage:[UIImage imageNamed:@"bouton_black_normal.png"]
  77.            forState:UIControlStateNormal];
  78. [favButton setBackgroundImage:[UIImage imageNamed:@"bouton_black_hightlight.png"]
  79.            forState:UIControlStateHighlighted];
  80. [favButton setTitle:@"Add" forState:UIControlStateNormal];
  81. [favButton setTitle:@"Add" forState:UIControlStateHighlighted];
  82. [favButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  83. [favButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
  84. [[favButton titleLabel] setFont:[UIFont boldSystemFontOfSize:13]];
  85. [favButton addTarget:self action:@selector(doSomething)
  86.     forControlEvents:UIControlEventTouchUpInside];
  87.  
  88. UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:favButton];
  89.        
  90. #define HIGHLIGHT_TIME 0.5f
  91.  
  92. @interface UIButton (Highlight)
  93. - (void)highlight;
  94. @end
  95.  
  96. @implementation UIButton (Highlight)
  97.  
  98. - (void)highlight {
  99.     UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
  100.     overlayButton.frame = self.frame;
  101.     overlayButton.showsTouchWhenHighlighted = YES;
  102.     [self.superview addSubview:overlayButton];
  103.     overlayButton.highlighted = YES;
  104.     [self performSelector:@selector(hideOverlayButton:) withObject:overlayButton afterDelay:HIGHLIGHT_TIME];
  105. }
  106.  
  107. - (void)hideOverlayButton:(UIButton *)overlayButton {
  108.     overlayButton.highlighted = NO;
  109.     [overlayButton removeFromSuperview];
  110. }
  111.  
  112. @end
  113.        
  114. UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)] autorelease];
  115. self.navigationItem.rightBarButtonItem = addButton;
  116. [self.navigationController.navigationBar.subviews.lastObject performSelector:@selector(highlight) withObject:nil afterDelay:2];