Unable to get [[id Animator] setAlphaValue: 0.0] to work

I am trying to animate opacity by decreasing + resizing the frame in NSImageView. FrameSize works smoothly, but for some reason, the alpha value simply goes straight to its final value at the beginning of the animation. I tried to return, relying on the supervisor, and my ideas are running out. Here is my sample code ->

AABLCLogo   -> NSImageView IBOutlet
speed       -> 1.0f 

//setting target frame for image view

NSRect newFrame      = [AABLCLogo frame];
newFrame.origin.x    = newFrame.origin.x + (newFrame.size.width*0.1);
newFrame.origin.y    = newFrame.origin.y + (newFrame.size.height*0.1);
newFrame.size.width  = newFrame.size.width * 0.8;
newFrame.size.height = newFrame.size.height * 0.8;

//backing layer for super view

[[AABLCLogo superview] setWantsLayer:YES];

//animating image view

[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration: speed];
[[AABLCLogo animator] setFrame: newFrame];
[[AABLCLogo animator] setAlphaValue: 0.0];
[NSAnimationContext endGrouping];

//XCode 4 delay snippet

double delayInSeconds   = speed;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 
                                        delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, 
               dispatch_get_main_queue(), 
               ^(void) {

    //code executes after ''speed'' seconds

    [[AABLCLogo superview] setWantsLayer:NO];
    [[self view] removeFromSuperview];

                });
+3
source share
3 answers

I suspect this is a problem with the animation in the main thread. In most cases, if you want to animate two things at once, you will need to use NSViewAnimation. Use this code as a guide; it resizes the window, reducing part of its contents.

NSRect newWindowFrame = NSMakeRect(self.window.frame.origin.x, self.window.frame.origin.y, 640, self.window.frame.size.height);
NSMutableArray *viewAnimations = [NSMutableArray array];
NSDictionary *windowSizeDict = [NSDictionary dictionaryWithObjectsAndKeys:self.window, NSViewAnimationTargetKey, [NSValue valueWithRect:newWindowFrame], NSViewAnimationEndFrameKey, nil];
[viewAnimations addObject:windowSizeDict];

NSDictionary *animateOutDict = [NSDictionary dictionaryWithObjectsAndKeys:self.dataSidebarController.containerView, NSViewAnimationTargetKey, NSViewAnimationFadeOutEffect, NSViewAnimationEffectKey, nil];
[viewAnimations addObject:animateOutDict];

NSViewAnimation *animation = [[NSViewAnimation alloc] initWithViewAnimations:viewAnimations];
[animation setDuration:0.35];
[animation setAnimationBlockingMode:NSAnimationNonblocking];
[animation setDelegate:self];
[animation startAnimation];
0
source

setWantsLayer: YES , .

NSView setAlphaValue: , NSView.

"" NSView setAlphaValue: Apple.

0

I believe that the layer you are requesting is not created until the next drawing of the view.

Thus, you need to either force the supervisor to display, or until the next iteration of the run loop before adding animations.

0
source

All Articles