Use destinationViewController button in a custom s

2019-08-02 13:51发布

My goal is to make a custom segue with a custom animation as follows: I want the segue to cover a button from the sourceViewController with a button from the destinationViewController with an effect similar to the navigation controller's push effect, i.e. the new button is supposed to push the old button away from right to left.

I have been able to make the old button (from the sourceViewController) move away as desired:

[UIView animateWithDuration:1.0 animations:^{
        // set the target frame for animated view
        sourceViewController.optionsButton.frame = leftButtonTargetFrame;
    } completion:^(BOOL finished) {
        [navigationController pushViewController:destinationViewController animated:NO];
        // reset the button's frame back to its original frame
        sourceViewController.optionsButton.frame = leftButtonInitFrame;
}];

But I am struggling to make the new button (from the destinationViewController) move in. The reason is that I cannot access the destinationViewController's view elements: While performing the segue they are not instantiated. And I cannot animate a button that is not instantiated.

So how can I replace a button in the sourceViewController with a button from the destinationViewController?

2条回答
淡お忘
2楼-- · 2019-08-02 14:02

You are correct that you cannot animate an object that does not yet exist. However, you can fake it.

  1. Create a place-holder button that will look identical to the button that will be in the new view controller.
  2. Animate it to the correct place.
  3. As the destination view controller comes in, its button should be invisible.
  4. After the the view controller is in place (i.e. the segue has finished) the destination view controller can ensure the proper placement if its button and make its actual button visible.

Hope this helps.

查看更多
地球回转人心会变
3楼-- · 2019-08-02 14:06

The view of the destination view controller hasn't been initialized/loaded at the time when you try to access the buttons. To load the view of the destination view controller, you can simply access the view property. Do this before using the buttons: [destinationViewController view];

destinationViewController.view; would also work, but it would generate a compiler warning.

Background Information:

If you access the view property and its value is currently nil, the view controller automatically calls the loadView method and returns the resulting view.

The method loadView loads the view that the controller manages. You should never call this method directly.

查看更多
登录 后发表回答