如何在 UIBarButtonItem 上触发高亮效果

发布于 2025-01-06 22:25:03 字数 178 浏览 0 评论 0原文

当您点击 UIToolbar 中的 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

入画浅相思 2025-01-13 22:25:03

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

highlight.png
屏幕截图

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

highlight.png
screenshot

筱果果 2025-01-13 22:25:03

以下方法假设您正在使用导航控制器的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];
                     }];
}

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];
                     }];
}
护你周全 2025-01-13 22:25:03

对于栏项目

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

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];
}
始终不够 2025-01-13 22:25:03

您可以尝试重新创建具有不同样式的新栏按钮项目,然后将其重新分配回来。我的实验是:

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

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

You can try to recreate a new bar button item with different style, and then reassign it back. My experiment was:

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

And in method highlight:

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

Your implementation might be different than mine, but as long as you are fine with reassigning the bar button, I think it'll work as expected. Good luck :)

当爱已成负担 2025-01-13 22:25:03

如果您有一个类别:

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

If you have a category:

#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

Then you need code like this…

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

…to highlight the button in the navigation bar after 2 seconds.

冷弦 2025-01-13 22:25:03

一个简单的方法是使用 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];

A simple ay is to init your UIBarButtonItem with a CustomView (UIBUtton)

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文