pmerino
pmerino

Reputation: 6130

How to make an slide animation while bringing an custom view in Cocoa?

I'm developing an app where when clicking to a button, custom view should slide from a side. Actually just a window appears, but I'd like to have something like iOS navigation controller. How this can be done? This is for an Mac OS X app.

Upvotes: 16

Views: 8189

Answers (4)

Jeba Moses
Jeba Moses

Reputation: 849

NSView *view = yourview;
view.wantsLayer = YES;

// Set the layer redraw policy. This would be better done in the initialization method of a NSView subclass instead of here.

 view.layerContentsRedrawPolicy = NSViewLayerContentsRedrawOnSetNeedsDisplay; 

Then,

 [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context)
                    {
                        context.duration = 2.f;
                        view.animator.frame = CGRectOffset(view.frame, 180, 0);
                    }
                    completionHandler:^{
                        view.hidden = YES;


                    }];

Upvotes: 1

VenoMKO
VenoMKO

Reputation: 3294

You can use animator. Here is a sample:

NSPoint startPoint = NSMakePoint(NSWidth([[self window] frame]), NSHeight([[self window] frame]) - NSHeight([view frame]));
[view setFrameOrigin:startPoint];
NSPoint endPoint = NSMakePoint(0.0f, startPoint.y);
[[view animator] setFrameOrigin:endPoint];

Upvotes: 5

silvansky
silvansky

Reputation: 2427

iain's answer is great! If it doesn't work, check if you call [prentView setWantsLayer:YES] somewhere in your code.

Upvotes: 1

iain
iain

Reputation: 5683

You can use a Core Animation transition. You need to turn on layer backing for the parent view, and then you can do

[[parentView animator] replaceSubview:oldView with:newView];

By default that will crossfade the views, but if you want to change it to a slide animation then you'd add the appropriate CATransition to the animations dictionary.

- (CATransition *)slideAnimation
{
    CATransition *transition = [CATransition animation];
    [transition setType:kCATransitionMoveIn];
    [transition setSubtype:kCATransitionFromRight];
    return transition;
}

and then to set that animation in your parentView

...
[parentView setAnimations:[NSDictionary dictionaryWithObject:[self slideAnimation] forKey:@"subviews"];
...

Upvotes: 23

Related Questions