开发者

how to prevent rotation of subview to portrayed mode

开发者 https://www.devze.com 2023-02-08 20:49 出处:网络
I am trying to add a sub view to the main view when a button is tapped. The main view supports both landscape modes and portrait mode.

I am trying to add a sub view to the main view when a button is tapped.

  • The main view supports both landscape modes and portrait mode.

  • The sub view supports onl开发者_如何学Goy landscape mode.

How do I go about doing this?


In - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration you could apply a transformation to the view you wish to not rotate:

view.transform = CGAffineTransformMakeRotation(M_PI/2);

You could put this in an animation block so that it follows the rotation animation:

[UIView setAnimationDuration:duration];
[UIView beginAnimations:nil context:NULL];
view.transform = CGAffineTransformMakeRotation(M_PI/2);
[UIView commitAnimations];

The rotation occurs around the views origin, which can make positioning it tricky. To position the view correctly I would first ignore the rotation and ensure that the struts and springs are set such that the view is in the correct position without the rotation. Once the view is positioned correctly re-enable the rotation and adjust the view's frame's origin so that the view is where you want it to be. It's possible to apply a translation transformation instead of adjusting the frame (transformations can be combined with CGAffineTransformConcat). Use which ever approach produces the most readable code.

Be careful, this kind of view manipulation can get confusing. Technically, the view that you do not want to rotate is actually the view that is rotating! You also may need to change the rotation amount depending on the start and end orientations.


Take a tag value to all the landscape views which ever you wanted to add and before adding them to the main view check for the tag value and add them.

You can know the current orientation by maintaining a flag whenever - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation is called.


Did you follow this link

iPhone app in landscape mode, 2008 systems

or just add this line of code in your .m file of the viewController which you want to display in landscape mode

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if(interfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        return YES;
    }
    return NO;
}


I resolve this by rotating the screen and adjusting the view frame.

if (toorientation == UIInterfaceOrientationPortrait) {
        sub_view.transform = CGAffineTransformMakeRotation(degreesToRadin(0));
    }
    else if (toorientation == UIInterfaceOrientationPortraitUpsideDown){
        sub_view.transform = CGAffineTransformMakeRotation(degreesToRadin(180));
    }
    else if (toorientation == UIInterfaceOrientationLandscapeLeft){
        sub_view.transform = CGAffineTransformMakeRotation(degreesToRadin(-90));
    }
    else if (toorientation == UIInterfaceOrientationLandscapeRight){
        sub_view.transform = CGAffineTransformMakeRotation(degreesToRadin(90));
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号