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

前端 未结 4 1742
礼貌的吻别
礼貌的吻别 2020-12-13 08:16

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

相关标签:
4条回答
  • 2020-12-13 08:24

    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"];
    ...
    
    0 讨论(0)
  • 2020-12-13 08:27

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

    0 讨论(0)
  • 2020-12-13 08:32

    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];
    
    0 讨论(0)
  • 2020-12-13 08:38
    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;
    
    
                        }];
    
    0 讨论(0)
提交回复
热议问题