简体   繁体   English

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

[英]how to make uiviews only rotate (transform) when orientation changes?

i'm looking to implement a behaviour similar to that of the ios camera app. 我正在寻求实现类似于ios相机应用程序的行为。 when the orientation is changed, the views and controls only rotate in place, but keep the same position. 更改方向后,视图和控件仅在原地旋转,而保持不变。 I've tried to set the positions like this 我试图设置这样的位置

- (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);
}
}

the problem with that solution is that the views animate their way to the "new" position, instead of keeping its position and only transform. 该解决方案的问题是视图将其动画方式设置为“新”位置,而不是保持其位置并且仅进行变换。

thank you for your time! 感谢您的时间! :) :)

I've found the solution. 我找到了解决方案。 To achieve this behaviour, one should set the "new" positions of the views in 为了实现这一行为,应在以下位置设置视图的“新”位置

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[UIView setAnimationsEnabled:NO];
// set the new positions ...
}

then, in 然后,在

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM