如何使uiviews只在方向改变时旋转(变换)?

时间:2014-04-22 08:35:29

标签: ios iphone objective-c uiview orientation

我正在寻求实现类似于ios相机应用程序的行为。更改方向时,视图和控件仅在适当位置旋转,但保持相同的位置。我试图设置这样的位置

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
CGRect newBottomControlsFrame;
CGRect newTopControlsFrame;
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
    CGFloat screenHeight = fmaxf(self.view.bounds.size.height, self.view.bounds.size.width);
    newBottomControlsFrame.origin.x = 0;
    newBottomControlsFrame.origin.y = screenHeight - 70;
    newBottomControlsFrame.size.height = 70;
    newBottomControlsFrame.size.width = 320;
    newTopControlsFrame.origin = CGPointMake(0, 0);
    newTopControlsFrame.size = CGSizeMake(320, 50);
    self.bottomControlsContainer.frame = newBottomControlsFrame;
    self.topControlsContainer.frame = newTopControlsFrame;
    NSLog(@"Frame: %f, %f, %f, %f", newBottomControlsFrame.origin.x,newBottomControlsFrame.origin.y,
          newBottomControlsFrame.size.width,newBottomControlsFrame.size.height);
} else {
    CGFloat screenHeight = fmaxf(self.view.bounds.size.height, self.view.bounds.size.width);
    newBottomControlsFrame.origin.x = screenHeight - 70;
    newBottomControlsFrame.origin.y = 0;
    newBottomControlsFrame.size.height = 320;
    newBottomControlsFrame.size.width = 70;
    newTopControlsFrame.origin = CGPointMake(0, 0);
    newTopControlsFrame.size = CGSizeMake(50, 320);
    self.bottomControlsContainer.frame = newBottomControlsFrame;
    self.topControlsContainer.frame = newTopControlsFrame;
    NSLog(@"Frame: %f, %f, %f, %f", newBottomControlsFrame.origin.x,newBottomControlsFrame.origin.y,
          newBottomControlsFrame.size.width,newBottomControlsFrame.size.height);
}
}

该解决方案的问题在于,视图会动画化为“新”位置,而不是保持其位置并仅进行转换。

谢谢你的时间! :)

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。要实现此行为,应在

中设置视图的“新”位置
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[UIView setAnimationsEnabled:NO];
// set the new positions ...
}

然后,在

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[UIView setAnimationsEnabled:YES];
// make a transform animation manually
}