Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fire hightlight effect on UIBarButtonItem

When you tap on a UIBarButtonItem in a UIToolbar, there is a white glow effect.

Is there a possibility to fire an event to show this effect?

I don't want to press the button. Only the effect should be displayed. I want to visualize to the user, that there is new content behind this button.

Thanks for your help!

like image 971
Manni Avatar asked Feb 20 '12 13:02

Manni


3 Answers

Heres the "highlight.png", I'm not kidding! (Though you may not seeing it on a white background.)

highlight.pngscreenshot

like image 54
PeakJi Avatar answered Nov 16 '22 10:11

PeakJi


The following method assumes you are using the toolbarItems property of a navigation controller and that the button you want to highlight is the last item in that array. You can apply it to any toolbar, of course, and pick a different array index if necessary.

Right now this is not exactly perfect, because the animation moves the new button from the lower left of the screen. However, I think that can be turned off with a little effort.

Note that I used PeakJi's image.

I hope this works for you.

Enjoy,

Damien

- (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];
                     }];
}
like image 35
Damien Del Russo Avatar answered Nov 16 '22 09:11

Damien Del Russo


For Bar items

 [(UIButton *)[[toolbarItems objectAtIndex:1] customView] setImage:[UIImage imageNamed:@"highlight.png"] forState:UIControlStateNormal];

In General - Assuming you have a button

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];

you can at any given point, programmatically call this function:

[button setTitle:@"Look Here" forState:UIControlStateNormal];

or if you like to have an highlight image

btnImage = [UIImage imageNamed:@"highlight.png"];
[button setImage:btnImage forState:UIControlStateNormal];

A very simple alternative:

That said , you can also set the button like this:

- (void)highlightButton:(UIButton *)button { 
    [button setHighlighted:YES];
}
like image 31
chewy Avatar answered Nov 16 '22 09:11

chewy