简体   繁体   中英

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. 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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