views:

46

answers:

0

Hi everybody!

I am trying to apply mask to my UIView. I have method on my UIView:

    - (void) applyMask :(CGRect)maskZone :(NSArray*)gradientColors :(CGFloat*)gradientLocations {
            CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();.

            CGContextRef context = CGBitmapContextCreate (NULL, self.bounds.size.width, self.bounds.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);

            CGContextTranslateCTM (context, 0, self.bounds.size.height);

            CGContextScaleCTM (context, 1.0f, -1.0f);

            CGContextSetBlendMode(context, kCGBlendModeCopy);

            CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);

            CGContextFillRect (context, self.bounds);

            CGGradientRef gradient = CGGradientCreateWithColors (colorSpace, (CFArrayRef)gradientColors, gradientLocations);                    

            CGContextDrawLinearGradient(context, gradient, CGPointMake(0, CGRectGetMinY (maskZone)), CGPointMake(0, CGRectGetMaxY (maskZone)), 0);


            CGImageRef contextImage = CGBitmapContextCreateImage(context);

            CGGradientRelease (gradient);
            CGColorSpaceRelease(colorSpace);
            CGContextRelease (context);

            CALayer* layerForMask = self.layer;

            CALayer* maskCompoudLayer = [[CALayer alloc] init];

            maskCompoudLayer.bounds = layerForMask.bounds;

            maskCompoudLayer.position = layerForMask.position;

            [maskCompoudLayer setContents :(id)contextImage];

            CGImageRelease (contextImage);

            layerForMask.mask = maskCompoudLayer;

            [maskCompoudLayer release];
    }

I call this method from layoutSubview methods and all works fine - I have exactly what I want, but when I rotate device mask is disappear. I can not understand why... layoutSubview method is being executed when orientation changes. So, I tried to execute this code in my controller:

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)orientation {
    [super didRotateFromInterfaceOrientation : orientation];
    [self.view setNeedsLayout];
}

And... it works!!! Why? Anybody, please explain me why I have to have this code in my controller?