我正在尝试使用 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];
}];
谢谢!
考虑在动画开始之前 View 的位置:
self.view.frame
是(我假设)0,0,320,380myViewController.view
是 self.view
myViewController.view.frame
是 320,0,320,480 在 self.view
的坐标系,所以它在其父 View 的框架之外(并离开屏幕的右边缘)现在考虑动画完成后 View 的位置:
self.view.frame
为 -320,0,320,480myViewController.view
仍然是 self.view
myViewController.view.frame
在 self.view
的坐标系中是 0,0,320,480 ,所以它完全在其父 View 的框架内,但是它在屏幕坐标中的框架是-320,0,320,480,所以它现在完全不在屏幕的左边缘您需要使 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 |