Search code examples
iosobjective-ciphonecgaffinetransform

Using CGAfflineTransformMakeScale/Rotation only does one action


I am trying to make a video rotate and scale larger when the user rotates the screen to landscape.

- (void) orientationChanged:(NSNotification *)note
{
    bool switchedLeft;
    UIDevice * device = note.object;
    switch(device.orientation)
    {
        case UIDeviceOrientationPortrait:
            self.videoView.transform=CGAffineTransformMakeScale(0.5,0.5);

            if (switchedLeft) {
                self.videoView.transform=CGAffineTransformMakeRotation(-M_PI_2);

            }else{
                self.videoView.transform=CGAffineTransformMakeRotation(M_PI_2);
            }
            break;
        case UIDeviceOrientationLandscapeLeft:
            self.videoView.transform=CGAffineTransformMakeRotation(M_PI_2);
            self.videoView.transform=CGAffineTransformMakeScale(2.0, 2.0);
            switchedLeft=true;
            break;

        case UIDeviceOrientationLandscapeRight:
            self.videoView.transform=CGAffineTransformMakeRotation(-M_PI_2);
            self.videoView.transform=CGAffineTransformMakeScale(2.0, 2.0);
            switchedLeft=false;
            break;

        default:
            break;
    };
}

There are a number of problems. First when I initially rotate to landscape it only does one transformation, in this configuration it just scales it.

The second problem is when I rotate to portrait it calls for the rotation but it never rotates. However i can go back and forth between landscape left and landscape right and it rotates properly. Any help would be greatly appreciated


Solution

  • You are essentially replacing the rotation transform with scale transform. In order to apply both, you need to use CGAffineTransformConcat().

    CGAffineTransform rotate = CGAffineTransformMakeRotation(M_PI_2);
    CGAffineTransform scale = CGAffineTransformMakeScale(2.0, 2.0);
    self.videoView.transform = CGAffineTransformConcat(rotate, scale);
    

    As for the second part, you don't need to apply another rotation, instead set it to default using CGAffineTransformIdentity.

    case UIDeviceOrientationPortrait:
        CGAffineTransform scale = CGAffineTransformMakeScale(0.5,0.5);
        self.videoView.transform = CGAffineTransformConcat(CGAffineTransformIdentity, scale);
        break;