NSSlider动画 [英] NSSlider animation

查看:229
本文介绍了NSSlider动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在更改浮动值时创建NSSlider动画。我尝试:

How can I create NSSlider animation when changing float value of it. I was trying:

[[mySlider animator] setFloatValue:-5];

但是没有工作..只是更改值没有动画。

but that didn't work.. just change the value without animation. So maybe someone knows how to do this?

推荐答案

确定 - 所以这不是一样快速和漂亮,我希望,但它的工作原理。
你实际上不能在滑块旋钮上使用animator和Core Animation - 因为Core Animation只能在图层上工作,并且不能访问滑块图层中的旋钮值。

Ok - so this isn't as quick and pretty as I hoped but it works. You can't actually use animators and Core Animation on the slider knob - because Core Animation works only on layers and there's no access to the knob values in the slider layer.

因此,我们必须改为手动动画滑动器值。
由于我们在Mac上执行此操作 - 您可以使用 NSAnimation (在iOS上不可用)。

So we have to resort instead to manually animating slider value. Since we're doing this on a Mac - you can use NSAnimation (which isn't available on iOS).

NSAnimation做的很简单 - 它提供了一个时间/插值机制,允许你动画(相对于Core Animation,它还连接到视图并处理更改他们)。

What NSAnimation does is simple - it provide an timing/interpolation mechanism to allow YOU to animate (as opposed to Core Animation which also connects to the views and handles the changes to them).

要使用NSAnimation - 最常见的是子类化并覆盖 setCurrentProgress:

To use NSAnimation - you most commonly would subclass it and override setCurrentProgress: and put your logic in there.

这是我实现这个 - 我创建了一个新的NSAnimation子类 NSAnimationForSlider

Here's how I implemented this - I created a new NSAnimation subclass called NSAnimationForSlider

NSAnimationForSlider.h:

NSAnimationForSlider.h :

@interface NSAnimationForSlider : NSAnimation  
{  
    NSSlider *delegateSlider;  
    float animateToValue;    
    double max;   
    double min;  
    float initValue;  
}  
@property (nonatomic, retain) NSSlider *delegateSlider;  
@property (nonatomic, assign) float animateToValue;    
@end  

NSAnimationForSlider.m:

NSAnimationForSlider.m :

#import "NSAnimationForSlider.h"

@implementation NSAnimationForSlider
@synthesize delegateSlider;
@synthesize animateToValue;

-(void)dealloc
{
    [delegateSlider release], delegateSlider = nil;
}

-(void)startAnimation
{
    //Setup initial values for every animation
    initValue = [delegateSlider floatValue];
    if (animateToValue >= initValue) {
        min = initValue;
        max = animateToValue;
    } else  {
        min = animateToValue;
        max = initValue;
    }

    [super startAnimation];
}


- (void)setCurrentProgress:(NSAnimationProgress)progress
{
    [super setCurrentProgress:progress];

    double newValue;
    if (animateToValue >= initValue) {
        newValue = min + (max - min) * progress;        
    } else  {
        newValue = max - (max - min) * progress;
    }

    [delegateSlider setDoubleValue:newValue];
}

@end

创建一个新的NSAnimationForSlider,给它一个滑块你正在作为一个委托,在每个动画之前你设置它的animateToValue,然后只是开始动画。

To use it - you simply create a new NSAnimationForSlider, give it the slider you are working on as a delegate and before each animation you set it's animateToValue and then just start the animation.

例如: / p>

For example:

slider = [[NSSlider alloc] initWithFrame:NSMakeRect(50, 150, 400, 25)];
[slider setMaxValue:200];
[slider setMinValue:50];
[slider setDoubleValue:50];

[[window contentView] addSubview:slider];

NSAnimationForSlider *sliderAnimation = [[NSAnimationForSlider alloc] initWithDuration:2.0 animationCurve:NSAnimationEaseIn];
[sliderAnimation setAnimationBlockingMode:NSAnimationNonblocking];
[sliderAnimation setDelegateSlider:slider];
[sliderAnimation setAnimateToValue:150];

[sliderAnimation startAnimation];

这篇关于NSSlider动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆