- How to fire hightlight effect on UIBarButtonItem
- - (void)highlightButton {
- NSArray *originalFooterItems = self.toolbarItems;
- NSMutableArray *footerItems = [originalFooterItems mutableCopy];
- [footerItems removeLastObject];
- NSString* pathToImageFile = [[NSBundle mainBundle] pathForResource:@"white_glow" ofType:@"png"];
- UIImage* anImage = [UIImage imageWithContentsOfFile:pathToImageFile];
- UIImageView *imageView = [[UIImageView alloc] initWithImage:anImage];
- imageView.frame = CGRectMake(0, 0, 40, 40);
- UIBarButtonItem *flashImage = [[UIBarButtonItem alloc] initWithCustomView:imageView];
- [footerItems addObject:flashImage];
- [UIView animateWithDuration:0.3
- delay: 0.0
- options: UIViewAnimationOptionCurveEaseOut
- animations:^{
- self.toolbarItems = footerItems; }
- completion:^(BOOL finished){
- [UIView animateWithDuration:.3
- delay: 0.0
- options:UIViewAnimationOptionCurveEaseIn
- animations:^{
- self.toolbarItems = originalFooterItems;
- }
- completion:nil];
- }];
- }
- [(UIButton *)[[toolbarItems objectAtIndex:1] customView] setImage:[UIImage imageNamed:@"highlight.png"] forState:UIControlStateNormal];
- UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- [button addTarget:self
- action:@selector(someFunction:)
- forControlEvents:UIControlEventTouchDown];
- [button setTitle:@"Click here" forState:UIControlStateNormal];
- button.frame = CGRectMake(0.0, 0.0, 100.0, 40.0);
- [self.view addSubview:button];
- [button setTitle:@"Look Here" forState:UIControlStateNormal];
- btnImage = [UIImage imageNamed:@"highlight.png"];
- [button setImage:btnImage forState:UIControlStateNormal];
- - (void)highlightButton:(UIButton *)button {
- [button setHighlighted:YES];
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"Custom"
- style:UIBarButtonItemStylePlain
- target:self
- action:nil];
- self.navigationItem.rightBarButtonItem = rightBtn;
- [rightBtn release];
- [self performSelector:@selector(highlight)
- withObject:nil
- afterDelay:2.0];
- }
- - (void) highlight{
- UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"Custom"
- style:UIBarButtonItemStyleDone
- target:self
- action:nil];
- self.navigationItem.rightBarButtonItem = rightBtn;
- [rightBtn release];
- }
- UIButton *favButton = [UIButton buttonWithType:UIButtonTypeCustom];
- [favButton setFrame:CGRectMake(0.0, 100.0, 50.0, 30.0)];
- [favButton setBackgroundImage:[UIImage imageNamed:@"bouton_black_normal.png"]
- forState:UIControlStateNormal];
- [favButton setBackgroundImage:[UIImage imageNamed:@"bouton_black_hightlight.png"]
- forState:UIControlStateHighlighted];
- [favButton setTitle:@"Add" forState:UIControlStateNormal];
- [favButton setTitle:@"Add" forState:UIControlStateHighlighted];
- [favButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
- [favButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
- [[favButton titleLabel] setFont:[UIFont boldSystemFontOfSize:13]];
- [favButton addTarget:self action:@selector(doSomething)
- forControlEvents:UIControlEventTouchUpInside];
- UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:favButton];
- #define HIGHLIGHT_TIME 0.5f
- @interface UIButton (Highlight)
- - (void)highlight;
- @end
- @implementation UIButton (Highlight)
- - (void)highlight {
- UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
- overlayButton.frame = self.frame;
- overlayButton.showsTouchWhenHighlighted = YES;
- [self.superview addSubview:overlayButton];
- overlayButton.highlighted = YES;
- [self performSelector:@selector(hideOverlayButton:) withObject:overlayButton afterDelay:HIGHLIGHT_TIME];
- }
- - (void)hideOverlayButton:(UIButton *)overlayButton {
- overlayButton.highlighted = NO;
- [overlayButton removeFromSuperview];
- }
- @end
- UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)] autorelease];
- self.navigationItem.rightBarButtonItem = addButton;
- [self.navigationController.navigationBar.subviews.lastObject performSelector:@selector(highlight) withObject:nil afterDelay:2];