当您在
谢谢您的帮助!
UIToolbar
中点击一个 UIBarButtonItem
时,会出现白色的发光效果。
有没有可能触发事件来显示这个效果?
我不想按下按钮。只想显示效果。我希望向用户展示,这个按钮后面有新内容。谢谢您的帮助!
UIToolbar
中点击一个 UIBarButtonItem
时,会出现白色的发光效果。
有没有可能触发事件来显示这个效果?
我不想按下按钮。只想显示效果。我希望向用户展示,这个按钮后面有新内容。- (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];
}
而在highlight方法中:
- (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秒后突出显示导航栏中的按钮。
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];