Search code examples
objective-canimationnswindowfacebook-pop

Is it possible to animate NSWindows with pop?


I want to use some of the nice looking pop animations on my application's NSWindow. I already do know how to animate all different kinds of CALayerBacked entities, but I can't figure out if there is a way to animate changes to the frame of my NSWindow for example.

The problem I'm confronted with is, that I can animate my NSWindow's contentView (since it is CALayerBacked), but not the position and size of the NSWindow itself.

I know, that I could simply call setFrame:display:animate:, but this does not offer the same smooth animation as the pop framework does.

kPOPViewFrame is not available when using OSX. I've to use kPOPLayerBounds instead:

enter image description here

Is there a way to achieve this?


Solution

  • You can create your own custom properties on any object you like using:

    + (id)propertyWithName:(NSString *)aName initializer:(void (^)(POPMutableAnimatableProperty *prop))aBlock

    You would set one up for NSWindow something like this...

    POPAnimatableProperty *windowPositionProperty = [POPAnimatableProperty propertyWithName:@"com.myname.NSWindow.position" initializer:^(POPMutableAnimatableProperty *prop) {
        prop.readBlock = ^(NSWindow *window, CGFloat values[]) {
            values[0] = window.frame.origin.x;
            values[1] = window.frame.origin.y;
        };
    
        prop.writeBlock = ^(NSWindow *window, const CGFloat values[]) {
            [window setFrameOrigin:CGPointMake(values[0], values[1])];
        };
    }];
    

    Note the documentation comment that "Custom properties should use reverse-DNS naming."

    And then setup the animation like this:

    POPSpringAnimation *endBounce = [POPSpringAnimation animation];
    endBounce.property = windowPositionProperty;
    endBounce.toValue = [NSValue valueWithCGPoint:targetPosition];
    [window pop_addAnimation:endBounce forKey:@"endBounce"];