我从左到右点击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];
}];
我想从右到左删除视图。任何帮助如何继续进行?
答案 0 :(得分:2)
您必须将子视图移出超级视图的帧,因此请将子视图的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];
}];
答案 1 :(得分:0)
您可以将其删除:
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*/}];
答案 2 :(得分:0)
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应该是类变量(属性)