使用动画从右到左移除视图

remove view from right to left with animations

我在从左到右单击 btw 时添加视图,想从右到左删除该视图。

下面是我从左到右添加视图时的代码

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main"
                                                         bundle: nil];

leftMenu = (LeftMenuViewController*)[mainStoryboard
                                                             instantiateViewControllerWithIdentifier: @"menuController"];



leftMenu.view.frame = CGRectMake(-320, 0, 320-50, self.view.frame.size.height);


[self addChildViewController:leftMenu];
[self.view addSubview:leftMenu.view];

[UIView animateWithDuration:0.3 animations:^{

    leftMenu.view.frame = CGRectMake(0, 0, 320-50, self.view.frame.size.height);
    hoverView.hidden = NO;


} completion:^(BOOL finished) {
    [leftMenu didMoveToParentViewController:self];
}];

为了从右到左删除此视图,我尝试过的是:

 self.view.frame = CGRectMake(320-50, 0, self.view.frame.size.width, self.view.frame.size.height);
[self willMoveToParentViewController:nil];


[UIView animateWithDuration:0.3 animations:^{

    self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);



} completion:^(BOOL finished) {

    [self removeFromParentViewController];
    [self.view removeFromSuperview];

}];

我想从右到左移除视图。对如何进一步进行有任何帮助吗?

您必须将您的子视图移出父视图的框架,因此将您的子视图的 x 坐标设置为其自身宽度的负值。这将导致您的视图从右向左移出视图。

[self willMoveToParentViewController:nil];

    [UIView animateWithDuration:0.3 animations:^{

        self.view.frame = CGRectMake(-self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
    } completion:^(BOOL finished) {

        [self removeFromParentViewController];
        [self.view removeFromSuperview];

    }];

您可以这样删除它:

    CGRect napkinBottomFrame = Yourview.frame;
 napkinBottomFrame.origin.x = 0;
 [UIView animateWithDuration:0.3 delay:0.0 options: UIViewAnimationOptionCurveEaseOut animations:^{ Yourview.frame = napkinBottomFrame; } completion:^(BOOL finished){/*done*/}];

Swift 3 和 4 版本:

leftMenu.didMove(toParentViewController: nil)
UIView.animate(withDuration: 0.3,
    animations: {
        self.leftMenu.view.frame = CGRect(x: -self.view.frame.width, y: 0, width: self.view.frame.width, height: self.view.frame.height)
    },
    completion: { finished in
        self.leftMenu.view.removeFromSuperview()                            
        self.leftMenu.removeFromParentViewController()
})

P.S。 leftMenu 应该是 class 变量 (属性)