简体   繁体   English

用动画从右到左删除视图

[英]remove view from right to left with animations

I adding view when btw is clicked from left to right and want to remove this view from right to left. 我从左到右单击btw并想从右到左删除此视图时添加视图。

Below is my code while adding view from left to right 下面是我的代码,同时从左到右添加视图

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];
}];

For removing this view from right to left what i have tried is: 为了从右到左删除此视图,我尝试过的是:

 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];

}];

I want to remove view from right to left .Any help how to proceed further? 我想从右到左删除视图。任何帮助如何进一步进行?

You must move your subview out of frame of superview, so set x-coordinate of your subview to negative value of it's own width. 您必须将子视图移出超级视图的框架,因此将子视图的x坐标设置为其自身宽度的负值。 This will cause your view move from right to left out of view. 这将导致视图从右移到左移。

[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];

    }];

You can remove it like this: 您可以这样删除它:

    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 and 4 version: 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()
})

PS leftMenu should be class variable (property) PS leftMenu应该是类变量(属性)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM