I'll take a hit:
I found a couple of problems here. Firstly, all this is configured to block, so it blocks the main thread for 5 seconds. This will translate to user as SPOD / freezes. You probably want this to be non-blocking, but this will require a little extra mechanism.
In addition, you are using NSAnimationEaseOut for a fading effect that occurs with a known bug that NSAnimationEaseOut animation to run in the opposite direction. (Google is for “NSAnimationEaseOut back,” and you can see that many have run NSAnimationEaseIn this problem.) I used NSAnimationEaseIn for both curves for this example.
I got this work for a trivial example with non-blocking animations. I'm not going to say that this is an ideal approach (I posted a second answer, which is probably better), but it works and, I hope, will be a jump for you. Here's the gist:
@interface NSTextField (AnimatedSetString) - (void) setAnimatedStringValue:(NSString *)aString; @end @interface SOTextFieldAnimationDelegate : NSObject <NSAnimationDelegate> - (id)initForSettingString: (NSString*)newString onTextField: (NSTextField*)tf; @end @implementation NSTextField (AnimatedSetString) - (void) setAnimatedStringValue:(NSString *)aString { if ([[self stringValue] isEqual: aString]) { return; } [[[SOTextFieldAnimationDelegate alloc] initForSettingString: aString onTextField: self] autorelease]; } @end @implementation SOTextFieldAnimationDelegate { NSString* _newString; NSAnimation* _fadeIn; NSAnimation* _fadeOut; NSTextField* _tf; } - (id)initForSettingString: (NSString*)newString onTextField: (NSTextField*)tf { if (self = [super init]) { _newString = [newString copy]; _tf = [tf retain]; [self retain];
It would be very nice if the API block were implemented for this ... it would save the need to implement this delegate object.
I put the whole project on GitHub .
source share