如何在UIBarButtonItem上实现高亮效果

5
当您在 UIToolbar 中点击一个 UIBarButtonItem 时,会出现白色的发光效果。 有没有可能触发事件来显示这个效果? 我不想按下按钮。只想显示效果。我希望向用户展示,这个按钮后面有新内容。
谢谢您的帮助!
6个回答

4
这里是“highlight.png”,我不是在开玩笑!(尽管在白色背景下可能看不到它。) highlight.png screenshot

8
我可以确认他不是在开玩笑 :) - Rok Jarc

2
以下方法假设您正在使用导航控制器的toolbarItems属性,并且您想要突出显示的按钮是该数组中的最后一项。当然,您可以将其应用于任何工具栏,并在必要时选择不同的数组索引。
目前这还不完美,因为动画会将新按钮从屏幕左下角移动。不过我认为只需花费一点点功夫即可关闭此功能。
请注意,我使用了PeakJi的图像。
希望这对您有所帮助。
祝使用愉快,
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];
                     }];
}

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

谢谢你的回答!工具栏中的白色发光效果是iOS SDK中的默认设置,我没有highlight.png文件,需要自己创建一个。难道没有其他方法可以触发默认的白色发光效果吗? - Manni
@Manni,我刚为你制作了一个highlight.png,哈哈 - PeakJi

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

而在highlight方法中:

- (void) highlight{
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"Custom"
                                                                 style:UIBarButtonItemStyleDone
                                                                target:self
                                                                action:nil];
    self.navigationItem.rightBarButtonItem = rightBtn;
    [rightBtn release];
}

你的实现可能与我的不同,但只要你愿意重新分配工具栏按钮,我认为它会按预期工作。祝你好运 :)


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秒后突出显示导航栏中的按钮。


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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接