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 naviga
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"];
...
iain's answer is great! If it doesn't work, check if you call [prentView setWantsLayer:YES] somewhere in your code.
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];
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;
}];