如何在选择后闪烁自定义 NSMenuItem 视图?
我需要将视图分配给 NSMenuItem 并进行一些自定义绘图。基本上,我在当前选定的菜单项旁边添加了一个小删除按钮等。但我希望我的自定义菜单项在所有其他方面看起来和行为都像常规菜单项一样。根据文档:
带有视图的菜单项不会绘制 它的标题、状态、字体或其他 标准绘图属性,以及 分配绘图责任 完全符合视图。
好吧,所以我必须复制状态列和选择渐变的外观,这并不难。我遇到问题的部分是菜单项在被选择后“闪烁”或“闪烁”的方式。我正在使用 NSTimer 尝试模仿这个小动画,但感觉不太好。它眨眼多少次?我应该使用什么时间间隔?我已经尝试了很多,但感觉不正常。
有没有人以前做过此操作或对如何向菜单项添加按钮有其他建议?也许应该有一个专门用于定制可可绘图的堆栈交换站点......
I need to assign a view to an NSMenuItem and do some custom drawing. Basically, I'm adding a little delete button next to the currently selected menu item, among other things. But I want my custom menu item to look and behave like a regular menu item in all other ways. According to the doc:
A menu item with a view does not draw
its title, state, font, or other
standard drawing attributes, and
assigns drawing responsibility
entirely to the view.
Ok, so I had to duplicate the look of the state column and the selection gradient, which wasn't that hard. The part I'm having trouble with is the way the menu item "flashes" or "blinks" after it is selected. I'm using an NSTimer to try to mimic this little animation, but it just feels off. How many times does it blink? What time interval should I use? I've experimented a lot and it just feels out of whack.
Has anyone done this before or have other suggestions on how to add a button to a menu item? Maybe there should be a stack exchange site just for custom cocoa drawing...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我知道这已经有一年多了,但这是我的谷歌搜索的第一次点击并且没有得到答复,所以我发布我的答案是为了那些仍在寻找解决方案的人。
对于我的应用程序,我将 Core Animation 与 NSMenuItem 视图的自定义 NSView 结合使用。我创建了一个新的图层支持视图,设置背景颜色,并将其添加到我的自定义视图中。然后我对图层(闪烁部分)进行动画处理。然后在
-(void)animationDidStop:(CAAnimation *)anim finish:(BOOL)flag
回调中,我删除了覆盖层并关闭了菜单。这并不完全匹配默认的 NSMenu 的 flash,但我想要一个 37Signals/Stack Overflow 黄色淡出技术< /a>,所以它对我有用。这是代码:I know this is over a year old, but this was the first hit on my Google search and was unanswered, so I'm posting my answer for sake of those still looking for a solution.
For my app, I used Core Animation with a custom NSView for the NSMenuItem view. I created a new layer-backed view, set the background color, and added it to my custom view. I then animated the layer (the flashing part). Then in the
-(void) animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
callback, I removed the overlay and closed the menu. This doesn't perfectly match the default NSMenu's flash, but I wanted a 37Signals/Stack Overflow Yellow Fade Technique, so it works for me. Here it is in code:实际上可以让自定义视图像常规 NSMenuItem 一样闪烁,而无需手动实现动画。
注意:这使用了私有 API,并且还修复了一些与自定义视图相关的其他奇怪的
NSMenuItem
怪癖。NSMenuItem.h
桥接标头
MenuItem.swift
这个 API 确实应该公开,如果您不是为 App Store 开发,那么它也许值得一看。
It is actually possible to have your custom view flash like a regular
NSMenuItem
without implementing the animation manually.Note: this uses a private API and also fixes a handful of other strange
NSMenuItem
quirks related to custom views.NSMenuItem.h
Bridging Header
MenuItem.swift
This API really ought to be public, and if you're not developing for the App Store, it might be worth having a look at.
这是我的代码,它闪烁自定义菜单项。
Here is my code that flashes a custom menu item.