Question

I make two images: one with my spinner image and another with mask image (partially transparent gradient). Imagine this: spinner image is rotating clockwise, in the bottom there is dark gradient (look like haze), rotated spinner partially hided with this gradient. That is my aim.

I add two UIImageView to my scene, but my mask image also influence to background colour and I don't want that. I want that my mask image masking only spinner, so I make this:

- (void)viewDidLoad
{
   [super viewDidLoad];

   //add mask to UIImageView
   CALayer *mask = [CALayer layer];
   mask.contents = (id)[[UIImage imageNamed:@"mask.png"] CGImage];
   mask.frame = CGRectMake(0, 0, self.spinner.bounds.size.width,self.spinner.bounds.size.height);
   self.spinner.layer.mask = mask;
   self.spinner.layer.masksToBounds = YES;



   //rotate UIImageView (trouble with mask, it must not be rotated)
   CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
   animation.fromValue = [NSNumber numberWithFloat:0.0f];
   animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
   animation.duration = 20.0f;
   animation.repeatCount = INFINITY;
   [self.spinner.layer addAnimation:animation forKey:@"Spinner"];

}

As a result I got I spinner with nice mask image, but my mask rotates with my spinner, I want to always clip my mask to bottom of my spinner UIImageView. How can I do this? Or my conception is wrong, if so, give me some directions how can I archive what I want. Feel free to ask. Thanks.

Was it helpful?

Solution

Was tricky for me, but I made it.

Here is what you need to make this: you need a UIView (as a container for UIImageView).

Change this part in source code that I posted below:

CALayer *mask = [CALayer layer];
mask.contents = (id)[[UIImage imageNamed:@"mask.png"] CGImage];
mask.frame = self.yourContainerView.bounds;

self.yourContainerView.layer.mask = mask;
self.yourContainerView.layer.masksToBounds = YES;

That's all. Hope this will help somebody.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top