我知道這已經過了一年多了,但這是我的谷歌搜索的第一次打擊,沒有回答,所以我張貼我的答案爲了那些仍在尋找解決方案的人。
對於我的應用程序,我使用Core Animation和NSMenuItem視圖的自定義NSView。我創建了一個新的圖層背景視圖,設置背景顏色,並將其添加到我的自定義視圖中。然後,我爲該圖層(閃爍的部分)進行了動畫處理。然後在-(void) animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
回調中,我刪除了疊加並關閉了菜單。這並不完全符合默認NSMenu的閃存,但我想要一個37Signals /堆棧溢出Yellow Fade Technique,所以它適用於我。這裏是代碼:
-(void) mouseUp:(NSEvent *)theEvent {
CALayer *layer = [CALayer layer];
[layer setDelegate:self];
[layer setBackgroundColor:CGColorCreateGenericRGB(0.0, 0.0, 1.0, 1.0)];
selectionOverlayView = [[NSView alloc] init];
[selectionOverlayView setWantsLayer:YES];
[selectionOverlayView setFrame:self.frame];
[selectionOverlayView setLayer:layer];
[[selectionOverlayView layer] setNeedsDisplay];
[selectionOverlayView setAlphaValue:0.0];
[self addSubview:selectionOverlayView];
CABasicAnimation *alphaAnimation1 = [CABasicAnimation animationWithKeyPath: @"alphaValue"];
alphaAnimation1.beginTime = 0.0;
alphaAnimation1.fromValue = [NSNumber numberWithFloat: 0.0];
alphaAnimation1.toValue = [NSNumber numberWithFloat: 1.0];
alphaAnimation1.duration = 0.07;
CABasicAnimation *alphaAnimation2 = [CABasicAnimation animationWithKeyPath: @"alphaValue"];
alphaAnimation2.beginTime = 0.07;
alphaAnimation2.fromValue = [NSNumber numberWithFloat: 1.0];
alphaAnimation2.toValue = [NSNumber numberWithFloat: 0.0];
alphaAnimation2.duration = 0.07;
CAAnimationGroup *selectionAnimation = [CAAnimationGroup animation];
selectionAnimation.delegate = self;
selectionAnimation.animations = [NSArray arrayWithObjects:alphaAnimation1, alphaAnimation2, nil];
selectionAnimation.duration = 0.14;
[selectionOverlayView setAnimations:[NSDictionary dictionaryWithObject:selectionAnimation forKey:@"frameOrigin"]];
[[selectionOverlayView animator] setFrame:[selectionOverlayView frame]];
}
-(void) animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
[selectionOverlayView removeFromSuperview];
NSMenuItem *enclosingMenuItem = [self enclosingMenuItem];
NSMenu *enclosingMenu = [enclosingMenuItem menu];
[enclosingMenu cancelTracking];
[enclosingMenu performActionForItemAtIndex:[enclosingMenu indexOfItem:enclosingMenuItem]];
}
當按下快捷鍵時(如果有),還應該在菜單欄中閃爍菜單項的父項。 – 2011-02-01 16:39:27
我正在尋找選定的項目漸變代碼(或只是開始和結束顏色),你能分享它嗎?提前致謝。 – 2011-03-05 14:24:29