Search code examples
iphonexcodeuiviewalphacgaffinetransform

Rotating a UIView with alpha = 0


I have a UIView that I rotate with this code:

helpView.transform = CGAffineTransformMakeRotation(degreesToRadians( rotationAngle ));

Where degreeToRadians just is a macro to convert from degrees to radians.

This works fine as long as the view is visible, eg alpha = 1. When I hide it (alpha = 0, which I animate) it does not rotate any more. I guess this is a smart way for the devices to "save" on drawing time, but is there any way I can force it to be drawn even when alpha is 0? Otherwise I will have to rotate it before I show it again.

Any good ideas?

Thanks

Edit: This is the code I use to show/hide the view.

-(void)showHelp
{
    bool helpAlpha = !helpView.alpha;
    CGFloat newScale;

    if (helpView.alpha) {
        newScale = kHelpSmall;
        helpView.transform = CGAffineTransformMakeScale(kHelpBig, kHelpBig);
    } else {
        newScale = kHelpBig;
        helpView.transform = CGAffineTransformMakeScale(kHelpSmall, kHelpSmall);
    }

    [UIView animateWithDuration:(kAnimationTimeShort / 2) animations:^(void) {
        [helpView setAlpha:helpAlpha];
        helpView.transform = CGAffineTransformMakeScale(newScale, newScale);
    }];
}

As you see I also scale it for a nicer effect. Works perfect when visible, does not rotate when alpha = 0. Rotation is done in another method, where I would prefer to keep it as I also rotate some other views there.


Solution

  • You are resetting the transform every time you use CGAffineTransformMake*. If you do this, you will get either a rotated transform or a scaled one. I am assuming the scaled one is after the rotated one and hence you aren't able to see the view rotated. If you need both the effects to remain, you will have to use CGAffineTransformRotate. So a scale and rotate will be

    helpView.transform = CGAffineTransformMakeScale(kHelpSmall, kHelpSmall);
    helpView.transform = CGAffineTransformRotate(helpView.transform, degreesToRadians(rotationAngle));
    

    The order might vary.