1

我在 NSStatusItem 对象中有一个自定义视图。此视图显示图标。它也可以显示进度,但你必须调用[self.statusitemview setProgressValue:theValue]; 我有一组图标,它会使用这个值选择正确的一个。

这看起来很生涩,因为执行的进程不会一直发送更新。所以我想动画这个。

我想像使用其他可可控件一样调用动画:[[self.statusItemView animator] 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: 绘制的,但具有流畅的动画...

4

2 回答 2

5

要为自定义属性设置动画,您需要使您的视图符合NSAnimatablePropertyContainer协议。

然后,您可以将多个自定义属性设置为动画(除了已经支持的属性NSView),然后您可以简单地使用视图的animator代理来为属性设置动画:

yourObject.animator.propertyName = finalPropertyValue;

除了使动画非常简单之外,它还允许您使用以下方法同时为多个对象设置动画NSAnimationContext

[NSAnimationContext beginGrouping];
firstObject.animator.propertyName = finalPropertyValue1;
secondObject.animator.propertyName = finalPropertyValue2;
[NSAnimationContext endGrouping];

您还可以设置持续时间并提供完成处理程序块:

[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0.5];
[[NSAnimationContext currentContext] setCompletionHandler:^{
    NSLog(@"animation finished");
}];
firstObject.animator.propertyName = finalPropertyValue1;
secondObject.animator.propertyName = finalPropertyValue2;
[NSAnimationContext endGrouping];

对于标准NSView对象,如果你想为视图中的属性添加动画支持,你只需要覆盖+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];
    }
}

我创建了一个简单的示例项目,展示了如何使用该NSAnimatablePropertyContainer协议同时为视图的多个属性设置动画。

成功更新视图所需要做的就是确保setNeedsDisplay:YES在修改任何可动画属性时调用它。然后,您可以在drawRect:方法中获取这些属性的值,并根据这些值更新动画。

于 2012-10-16T01:55:07.137 回答
-1

在这里回答了类似的问题

您不能使用 animator 为自定义属性设置动画,但如果需要,您可以编写自定义动画,但这不是最好的主意。

更新(自定义动画):

- (void) scrollTick:(NSDictionary*) params
{
  static NSTimeInterval timeStart = 0;
  if(!timeStart)
    timeStart = [NSDate timeIntervalSinceReferenceDate];

  NSTimeInterval stTime = timeStart;
  NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate];
  NSTimeInterval totalTime = [[params valueForKey:@"duration"] doubleValue];

  if(currentTime > timeStart + totalTime)
  {
      currentTime = timeStart + totalTime;
      timeStart = 0;
  }

  double progress = (currentTime - stTime)/totalTime;
  progress = (sin(progress*3.14-3.14/2.0)+1.0)/2.0;

  NSClipView* clip = [params valueForKey:@"target"];
  float startValue = [[params valueForKey:@"from"] floatValue];
  float endValue = [[params valueForKey:@"to"] floatValue];

  float newValue = startValue + (endValue - startValue)*progress;
  [self setProperty:newValue];

  if(timeStart)
    [self performSelectorOnMainThread:@selector(scrollTick:) withObject:params waitUntilDone:NO];
}

- (void) setAnimatedProperty:(float)newValue
{
  NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:self.property], @"from",
   [NSNumber numberWithFloat:newValue],@"to",
   [NSNumber numberWithFloat:1.0],@"duration",
            nil];

  [self performSelectorOnMainThread:@selector(scrollTick:) withObject:params waitUntilDone:NO];
}
于 2012-10-14T03:49:22.510 回答