简体   繁体   English

如何使用动画更改NSTableColumn宽度

[英]How to Change NSTableColumn width with Animation

I have a NSScrollView and a NSTableView with some NSTableColumn in it. 我有一个NSScrollView和一个带有一些NSTableColumn的NSTableView。 When I push a button,I change the NSScrollView'width with Animation. 当我按下按钮时,我用动画更改了NSScrollView的宽度。 Here is my code: 这是我的代码:

NSDictionary *myScrollViewDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
                                               myScrollView,NSViewAnimationTargetKey,
                                               [NSValue valueWithRect:myScrollViewStartFrame],NSViewAnimationStartFrameKey,
                                               [NSValue valueWithRect:myScrollViewEndFrame],NSViewAnimationEndFrameKey, nil];

    NSViewAnimation *animation = [[NSViewAnimation alloc] initWithViewAnimations:@[myScrollViewDictionary]];
    animation.delegate = self;
    animation.duration = 0.2;
    [animation setAnimationBlockingMode:NSAnimationBlocking];
    [animation startAnimation];

This works fine. 这很好。

But I want to change each Column'width in the meanwhile with the same Animation,the problem is NSTableColumn is not a NSView, so I can not change its frame.Now I only can change Column'width without any Animation. 但是我想同时用相同的Animation改变每列的宽度,问题是NSTableColumn不是NSView,所以我不能改变它的框架。现在我只能在没有任何Animation的情况下改变Column的宽度。

somebody have ideas? 有人有主意吗?

I'm afraid you would probably have to create your own custom animation that does everything manually. 恐怕您可能必须创建自己的自定义动画,以手动完成所有操作。 To animate the column, here is a general idea: 要为该列设置动画,这是一个基本思路:

- (IBAction)animateColumn:(id)sender
{
  NSTableColumn *tableColumn; // = set your table column
  CGFloat initialWidth = [tableColumn width];
  CGFloat finalWidth; // = whatever

  dispatch_async(dispatch_get_global_queue(0,0), {
    double animationTime = 2; //seconds
    double timeElapsed = 0; //approximate measure; probably good enough

    while (timeElapsed<animationTime)
    {
      double t = timeElapsed/animationTime;

      CGFloat width = t*finalWidth + (1-t)*initialWidth;
      dispatch_async(dispatch_get_main_queue(), ^{
        [tableColumn setWidth:width];
      });

      NSDate *future = [NSDate dateWithTimeIntervalSinceNow:0.02];
      [NSThread sleepUntilDate:future];
      timeElapsed += 0.02;
    }

    dispatch_async(dispatch_get_main_queue(), ^{
      [tableColumn setWidth:finalWidth];
    });
  });
}

I guess you can adapt this to do what you want. 我想您可以根据自己的需要进行调整。 Perhaps you can start this animation at the same time you start the other animation (using the same time for both, changing 2 to 0.2 in my example). 也许您可以在启动另一个动画的同时启动该动画(在两者中使用相同的时间,在我的示例中将2更改为0.2)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM