Search code examples
iosxcodeuiinterfaceorientationcgaffinetransformscreen-rotation

Rotate the view only in ios


My application is only supported on Portrait mode. I have requirement that need to rotate the particular UIView only according to the device orientation.

Can you please help me?


Solution

  • After the long try I found the answer using CGAffineTransform ..

    - (void)deviceOrientationDidChange:(NSNotification *)notification {
        //Obtain current device orientation
        UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    
        CGFloat angle;
        switch (orientation) {
            case UIDeviceOrientationPortraitUpsideDown:
                angle = M_PI;
                break;
            case UIDeviceOrientationLandscapeRight:
                angle = - M_PI_2;
                break;
            case UIDeviceOrientationLandscapeLeft:
                angle = M_PI_2;
                break;
            case UIDeviceOrientationPortrait:
            default:
                angle = 0.f;
                break;
        }
        animationView.transform= CGAffineTransformMakeRotation(angle);
        animationView.frame = self.view.frame;
    }