【发布时间】:2012-03-01 01:30:16
【问题描述】:
当您在UIToolbar 中点击UIBarButtonItem 时,会出现白光效果。
是否有可能触发一个事件来显示这种效果?
我不想按下按钮。只应显示效果。我想向用户展示这个按钮后面有新内容。
感谢您的帮助!
【问题讨论】:
标签: iphone objective-c ios uibarbuttonitem
当您在UIToolbar 中点击UIBarButtonItem 时,会出现白光效果。
是否有可能触发一个事件来显示这种效果?
我不想按下按钮。只应显示效果。我想向用户展示这个按钮后面有新内容。
感谢您的帮助!
【问题讨论】:
标签: iphone objective-c ios uibarbuttonitem
这是“highlight.png”,我不是在开玩笑! (虽然你可能看不到它在白色背景上。)
【讨论】:
以下方法假定您正在使用导航控制器的 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];
}];
}
【讨论】:
对于酒吧项目
[(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];
}
【讨论】:
您可以尝试重新创建一个具有不同样式的新栏按钮项,然后重新分配它。我的实验是:
在 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];
}
您的实现可能与我的不同,但只要您可以重新分配条形按钮,我认为它会按预期工作。祝你好运:)
【讨论】:
如果你有一个类别:
#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 秒后突出显示导航栏中的按钮。
【讨论】:
一个简单的方法是用 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];
【讨论】: