- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 NSStatusItem 对象中有一个自定义 View 。该 View 显示图标。它还可以显示进度,但您必须调用 [self.statusitemview setProgressValue:theValue];
我有一组图标,它使用该值选择正确的图标。
这看起来非常不稳定,因为执行的进程并不总是发送更新。所以我想将其动画化。
我想像使用其他 cocoa 控件一样调用动画:[[self.statusItemView 动画师] setProgressValue:value];
如果可以的话
执行此操作的正确方法是什么?我不想使用 NSTimer。
编辑
图像是使用drawRect:方法绘制的
代码如下:
- (void)drawRect:(NSRect)dirtyRect
{
if (self.isHighlighted) {
[self.statusItem drawStatusBarBackgroundInRect:self.bounds withHighlight:YES];
}
[self drawIcon];
}
- (void)drawIcon {
if (!self.showsProgress) {
[self drawIconWithName:@"statusItem"];
} else {
[self drawProgressIcon];
}
}
- (void)drawProgressIcon {
NSString *pushed = (self.isHighlighted)?@"-pushed":@"";
int iconValue = ((self.progressValue / (float)kStatusItemViewMaxValue) * kStatusItemViewProgressStates);
[self drawIconWithName:[NSString stringWithFormat:@"statusItem%@-%d", pushed, iconValue]];
}
- (void)drawIconWithName:(NSString *)iconName {
if (self.isHighlighted && !self.showsProgress) iconName = [iconName stringByAppendingString:@"-pushed"];
NSImage *icon = [NSImage imageNamed:iconName];
NSRect drawingRect = NSCenterRect(self.bounds, icon);
[icon drawInRect:drawingRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:YES hints:nil];
}
- (void)setProgressValue:(int)progressValue {
if (progressValue > kStatusItemViewMaxValue || progressValue < 0) {
@throw [NSException exceptionWithName:@"Invalid Progress Value"
reason:[NSString stringWithFormat:@"The value %d id invalid. Range {0 - %d}", progressValue, kStatusItemViewMaxValue]
userInfo:nil];
}
_progressValue = progressValue;
[self setNeedsDisplay:YES];
}
- (void)setShowsProgress:(BOOL)showsProgress {
if (!showsProgress) self.progressValue = 0;
_showsProgress = showsProgress;
[self setNeedsDisplay:YES];
}
这一定是可能的。由于Apple 的标准控件是使用drawRect: 绘制的,但具有流畅的动画...
最佳答案
要为自定义属性设置动画,您需要使 View 符合NSAnimatablePropertyContainer
协议(protocol)。
然后,您可以将多个自定义属性设置为可动画(除了 NSView
已经支持的属性之外),然后您可以简单地使用 View 的 animator
代理为属性设置动画:
yourObject.animator.propertyName = finalPropertyValue;
除了使动画变得非常简单之外,它还允许您使用 NSAnimationContext
同时为多个对象设置动画:
[NSAnimationContext beginGrouping];
firstObject.animator.propertyName = finalPropertyValue1;
secondObject.animator.propertyName = finalPropertyValue2;
[NSAnimationContext endGrouping];
您还可以设置持续时间并提供完成处理程序 block :
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0.5];
[[NSAnimationContext currentContext] setCompletionHandler:^{
NSLog(@"animation finished");
}];
firstObject.animator.propertyName = finalPropertyValue1;
secondObject.animator.propertyName = finalPropertyValue2;
[NSAnimationContext endGrouping];
对于标准的NSView
对象,如果你想给 View 中的某个属性添加动画支持,你只需要重写 View 中的+defaultAnimationForKey:
方法并返回属性的动画:
//declare the default animations for any keys we want to animate
+ (id)defaultAnimationForKey:(NSString *)key
{
//in this case, we want to add animation for our x and y keys
if ([key isEqualToString:@"x"] || [key isEqualToString:@"y"]) {
return [CABasicAnimation animation];
} else {
// Defer to super's implementation for any keys we don't specifically handle.
return [super defaultAnimationForKey:key];
}
}
我创建了一个简单的sample project展示了如何使用 NSAnimatablePropertyContainer
协议(protocol)同时为 View 的多个属性设置动画。
要成功更新,您的 View 所需要做的就是确保在修改任何可动画属性时调用 setNeedsDisplay:YES
。然后,您可以在 drawRect:
方法中获取这些属性的值,并根据这些值更新动画。
关于cocoa - 动画 NSStatusItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12834092/
我是一名 .Net 开发人员,需要将一个小程序移植到 Mac OS X。我已经完成了大部分工作(部分感谢此网站上的人员,谢谢!),但有一个错误,也许我可以得到帮助与。 我正在创建一个位于状态栏中的工具
我正在开发一个在 Mac OS 状态栏中带有图标的应用程序。 我添加我的图标: self.statusItem = [[NSStatusBar systemStatusBar] statusItemW
我在 NSStatusItem 对象中有一个自定义 View 。该 View 显示图标。它还可以显示进度,但您必须调用 [self.statusitemview setProgressValue:th
我有一个 NSStatusItem,我想通过右键单击该项目来弹出一个菜单。我对 NSView 进行了子类化并覆盖了 - (void)rightMouseDown:(NSEvent *)event 方法
我在启动时显示一个 statusItem,如下所示: theItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariable
我正在考虑一个想法,基本上我想要一个带有 NSPopoverController 的 NSStatusItem 。我读到了人们遇到的所有问题,但我只是想尝试一下。现在有干净的方法吗?我见过的所有版本都
我是一名 .Net 开发人员,需要将一个小项目移植到 Mac 中,所以我对 Objective C 几乎一无所知。事实上,下面的代码只是一堆捕获救命稻草和在黑暗中拍摄的代码。 尝试构建一个状态菜单程序
我使用黑色和透明的 PNG 创建状态项。我看到许多应用程序在突出显示时将颜色图标更改为白色(默认为蓝色背景),我想知道这个过程是自动的还是我必须调用 setAlternateImage 来手动更改它。
你们都知道 Mac OS X 中的菜单栏(或者更好地说 NSStatusBar)。 有些元素我可以移动,有些则不能。 我希望能够拖动我的应用程序的 NSStatusItem。 知道如何实现这个吗? 最
我有一个可变长度的NSStatusItem,我希望尽可能保持可见,即使这意味着只显示部分内容,但当我的项目足够宽以运行到应用程序的菜单栏,它完全隐藏。有没有办法知道何时发生这种情况,以便我可以缩小 V
我正在为 OS X 编写一个状态栏应用程序,我想在等待下载文件时使用动画“正在加载”gif 作为图标。 将 NSImage 中的 gif 传递给 NSStatusItem 的 setImage: 方法
我有一个带有附加菜单的 NSStatusItem,我使用全局热键触发该菜单。如果我单击菜单项,我会像平常一样突出显示,如果我使用热键,则不会触发突出显示。有谁知道触发高亮的方法吗? 我尝试过重写 Vi
我的菜单栏应用程序如何实现与 1Password 或 Dropbox 相同的行为: 单击他们的菜单栏图标或弹出框不会窃取焦点,例如例如,当我在 TextEdit 中并打开 1Password/Drop
我在名为 StatusMenuController 的类中创建一个 NSStatusItem,如下所示 class StatusMenuController: NSObject { let statu
可以将 NSStatusItem 的 behaviour 属性设置为 removalAllowed (NSStatusItemBehaviorRemovalAllowed)然后,用户可以使用拖放操作从
首先:我是 Swift 开发的新手。 我正在使用 this 教程制作一个弹出窗口,当我点击我的顶部栏图标时会出现该弹出窗口。 所以我的 AppDelehgate 代码是: func applicati
我已经为我的应用程序创建了一个 NSStatusItem,但希望它在使用 Control+F8 (Control+fn+f8) 时可以导航,就像系统项一样。 状态项在导航序列中被莫名其妙地跳过了。是否
我正在制作一个状态栏应用程序,它在单击 NSStatusItem 时显示 NSPopover,如下所示: 我添加了通过拖动边缘来调整弹出窗口大小的功能,方法是像这样子类化弹出窗口的 View : cl
我有一个仅限 NSStatusItem 的小型应用程序,带有设置窗口。从 NSStatusItem 中的菜单中,我想重新激活主窗口。 应用程序保留一个NSWindow实例,该实例设置为关闭时不释放。关
我有一个 OSX 应用程序,除了 NSStatusBar 中显示的图标之外,它还有自己的窗口。 NSStatusBar 的原因是使应用程序能够在主窗口关闭时在后台运行。当尝试单击状态栏图标时,我必须单
我是一名优秀的程序员,十分优秀!