我将向您展示两种方法:第一个“redefineLayout”计算元素的新位置,第二个是旋转后的钩子“didRotateFromInterfaceOrientation”。
- (void)redefineLayout
{
int margin = 20;
int buttonWidth = ((int)self.view.frame.size.width - margin * 4)/3;
[self.buttonNewOrder setFrame:CGRectMake(margin, 20, buttonWidth, 200)];
[self.buttonStatistics setFrame:CGRectMake(margin * 2 + buttonWidth, 20, buttonWidth, 200)];
[self.buttonSincronize setFrame:CGRectMake(margin * 3 + buttonWidth * 2, 20, buttonWidth, 200)];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self redefineLayout];
}
我的界面可以工作,但在旋转之后,我看到元素做出了快速而难看的运动。我想让这个动作更加流畅。有什么建议吗?
我的解决方案:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self redesignLayout:duration];
}
- (void)redesignLayout:(NSTimeInterval)duration {
[UIView animateWithDuration:duration animations:^{
int margin = 20;
int buttonWidth = (((int)self.view.frame.size.width) - margin * 4)/3;
[self.buttonNewOrder setFrame:CGRectMake(margin, 20, buttonWidth, 200)];
[self.buttonStatistics setFrame:CGRectMake(margin * 2 + buttonWidth, 20, buttonWidth, 200)];
[self.buttonSincronize setFrame:CGRectMake(margin * 3 + buttonWidth * 2, 20, buttonWidth, 200)];
}];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self initLayout];
[self redesignLayout:0];
}