Setting a rotation transformation to a UIView or i

2019-02-09 06:49发布

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 it should, and I'm trying to apply a 90-degree rotation to the one "stationary" view to counteract the overall rotation.

Problem is, everything seems to rotate anyways, and the transform doesn't seem to do anything. I've attempted with an affine transform on the view, and with a 3d transform on the layer (below). The method is getting called, but I never see a visual difference.

Any thoughts? Thanks.

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
    CALayer *layer = stuckview.layer;
    layer.transform = CATransform3DMakeRotation(90, 0, 0, 1);
}    

2条回答
Anthone
2楼-- · 2019-02-09 07:02

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.

查看更多
神经病院院长
3楼-- · 2019-02-09 07:17

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;
        }
    }
查看更多
登录 后发表回答