Setting a rotation transformation to a UIView or its layer doesn't seem to work?

前端 未结 2 554
再見小時候
再見小時候 2021-01-02 05:53

I\'m trying to have one of the children views in my screen (owned by one view controller) not rotate when the device rotates. My view controller allows rotations as

相关标签:
2条回答
  • 2021-01-02 06:38

    Is your code actually executed? (Do you implement shouldAutorotateToInterfaceOrientation: ?)

    stuckview.transform = CGAffineTransformMakeRotation(M_PI_2); 
    

    should do the job.

    Note: The functions take radians not degrees.

    0 讨论(0)
  • 2021-01-02 06:54

    To help others find this, I'm adding a couple searchable phrases, like:

    prevent a UIView from rotating

    prevent a UITableView background from rotating

    stop a UIView rotation

    stop a UITableView background rotation


    A complete sample for any orientation:

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
        {
            switch (toInterfaceOrientation) {
                case UIInterfaceOrientationLandscapeLeft:
                    stuckview.transform = CGAffineTransformMakeRotation(M_PI_2); // 90 degress
                    break;
                case UIInterfaceOrientationLandscapeRight:
                    stuckview.transform = CGAffineTransformMakeRotation(M_PI + M_PI_2); // 270 degrees
                    break;
                case UIInterfaceOrientationPortraitUpsideDown:
                    stuckview.transform = CGAffineTransformMakeRotation(M_PI); // 180 degrees
                    break;
                default:
                    stuckview.transform = CGAffineTransformMakeRotation(0.0);
                    break;
            }
        }
    
    0 讨论(0)
提交回复
热议问题