如何在UIBarButtonItem上触发高亮效果

时间:2012-02-20 13:05:19

标签: iphone objective-c ios uibarbuttonitem

当您点击UIBarButtonItem中的UIToolbar时,会出现白色发光效果。

是否有可能触发事件以显示此效果?

我不想按下按钮。只应显示效果。我想向用户显示该按钮背后有新内容。

感谢您的帮助!

6 个答案:

答案 0 :(得分:4)

继承人“highlight.png”,我不是在开玩笑! (虽然你可能没有在白色背景上看到它。)

highlight.png screenshot

答案 1 :(得分:2)

以下方法假定您使用的是导航控制器的toolbarItems属性,并且要突出显示的按钮是该数组中的最后一项。当然,您可以将它应用于任何工具栏,并在必要时选择不同的数组索引。

现在这并不完美,因为动画会从屏幕的左下方移动新按钮。但是,我认为可以通过一点努力来关闭它。

请注意,我使用了PeakJi的图像。

我希望这适合你。

享受,

达明

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

答案 2 :(得分:1)

对于酒吧物品

 [(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];
}

答案 3 :(得分:1)

您可以尝试使用不同的样式重新创建新的条形按钮项,然后重新分配。我的实验是:

在viewDidLoad中:

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

您的实施可能与我的不同,但只要您重新分配条形按钮,我认为它将按预期工作。祝你好运:)

答案 4 :(得分:1)

如果您有类别:

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

...在2秒后突出显示导航栏中的按钮。

答案 5 :(得分:0)

一个简单的方法是使用CustomView(UIBUtton)初始化您的UIBarButtonItem

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