NSAnimation不会让我用动画的方式改变NSColor。我该怎么做呢?
发布于 2012-11-30 15:36:19
您可以使用blendedColorWithFraction:ofColor:。在你的动画方法中(无论哪个方法处理动画的当前值,如果你没有一个,只需创建一个):
NSColor *startColor = [NSColor redColor];
NSColor *targetColor = [NSColor blueColor];
float progress = [animation currentValue];
NSColor *currentColor = [startColor blendedColorWithFraction:progress ofColor:targetColor];编辑:
您可以做的是创建NSAnimation的子类。您的子类只需要覆盖setCurrentProgress:方法,以确定您在动画中走了多远。您可以完全按照相同的方式配置动画的其余部分。这个协议在这个场景中可能有点夸张,但是它为您的子类动画提供了一种专用的方法,可以将NSColor返回给创建该动画的类实例。
@protocol MyAnimationTarget
- (void) setColorOfSomething:(NSColor *);
@end
@interface MyAnimation : NSAnimation
@property id<MyAnimationTarget> target;
@property NSColor *color1;
@property NSColor *color2;
@end
@implementation MyAnimation
@synthesize target = _target;
@synthesize color1 = _color1;
@synthesize color2 = _color2;
- (void) setCurrentProgress:(NSAnimationProgress) d
{
[super setCurrentProgress:d];
NSColor *currentColor = [self.color1 blendedColorWithFraction:d ofColor:self.color2];
[self.target setColorOfSomething:currentColor];
}
@end在您的其他代码中:
MyAnimation *myAnim = [[MyAnimation alloc] init];
myAnim.target = self; // assuming self has a setColorOfSomething: method
myAnim.color1 = [NSColor redColor];
myAnim.color2 = [NSColor blueColor];
// set up other animation stuff
[myAnim startAnimation];发布于 2012-11-30 15:38:26
这只是个建议。也许你可以使用淡入淡出,如下所示:
begin animation
obj.alpha = 0.0;
commit animation
begin animator
obj.color = newColor;
obj.alpha = 1.0;
commit animationhttps://stackoverflow.com/questions/13640698
复制相似问题