How can I slide a view in and out of a window in a Cocoa program

后端 未结 4 667
慢半拍i
慢半拍i 2020-12-28 21:40

I want to have a view on a window and in response to a message (button click or menu) I want to have another view slide down above it, and have the first view resize.

<
4条回答
  •  长发绾君心
    2020-12-28 21:46

    CoreAnimation is definitely your best bet. It has been a while since I've worked with any CA code, but something like:

    [UIView beginAnimations:@"slideOn" context:nil];
    
    firstView.frame = shrunkFirstViewRect; // The rect defining the first view's smaller frame. This should resize the first view
    
    secondView.frame = secondViewOnScreenFrame; // This should move the second view on the frame. 
    
    [UIView commitAnimations];
    

    Later, you could return to a single view using:

    [UIView beginAnimations:@"slideOff" context:nil];
    
    firstView.frame = normalFirstViewRect; // The rect defining the first view's normal frame. This should expand the first view.
    
    secondView.frame = secondViewOffScreenFrame; // Move the second view off the screen
    
    [UIView commitAnimations];
    

    Edit: The above code is for the iPhone, I read your question a bit quick.

    On the Mac, you would want to use (similarly):

    [NSAnimationContext beginGrouping];
    [[NSAnimationContext currentContext] setDuration:1.0f]; // However long you want the slide to take
    
    [[firstView animator] setFrame:shrunkFirstViewRect];
    
    [[secondView animator] setFrame:secondViewOnScreenFrame];
    
    [NSAnimationContext endGrouping];
    

提交回复
热议问题