OStack程序员社区-中国程序员成长平台

标题: ios - 动画self.view.frame? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 18:21
标题: ios - 动画self.view.frame?

我正在尝试使用 UIView 动画来模仿 UINavigationController 的 pushViewController,但我似乎遇到了问题。我无法为 self.view.frame 设置动画。

这是我正在使用的代码,但 self.view 不会移动!

    [self.view addSubview:myViewController.view];
    [myViewController.view setFrame:CGRectMake(320, 0, 320, 480)];    
    [UIView animateWithDuration:0.5 
                     animations:^{
                         [self.view setFrame:CGRectMake(-320, 0, 320, 480)];
                         [myViewController.view setFrame:CGRectMake(0, 0, 320, 480)];
                     }
                     completion:^(BOOL finished){
                         [view1.view removeFromSuperview];
                     }];

谢谢!



Best Answer-推荐答案


考虑在动画开始之前 View 的位置:

现在考虑动画完成后 View 的位置:

您需要使 myViewController.view 成为 self.view 的兄弟,而不是 subview 。试试这个:

// Calculate the initial frame of myViewController.view to be
// the same size as self.view, but off the right edge of self.view.
// I don't like hardcoding coordinates...
CGRect frame = self.view.frame;
frame.origin.x = CGRectGetMaxX(frame);
myViewController.view.frame = frame;
[self.view.superview addSubview:myViewController.view];
// Now slide the views over.
[UIView animationWithDuration:0.5 animations:^{
    CGRect frame = self.view.frame;
    myViewController.view.frame = frame;
    frame.origin.x -= frame.size.width;
    self.view.frame = frame;
} completion:^(BOOL done){
    [view1.view removeFromSuperview];
}];

关于ios - 动画self.view.frame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8321253/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4