我将向您展示两种方法:第一种方法“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];
}
答案 0 :(得分:0)
尝试用此替换didRotateFromInterfaceOrientation
来电:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration
{
[UIView animateWithDuration:duration animations:^{
[self redefineLayout];
}];
}