Search code examples
iphoneioscore-animationcalayermask

Animation on a layer that doesn't want to be removed


I have a mask that I've build thanks to a layer and an image. I want to move this layer with little "jumps" (without a nice translation animation).

The problem is that there is an animation happening whenever I move the frame of the mask layer. I've tried calling [maskLayer removeAllAnimations]; but it doesn't change anything.

In my code, I don't put any animation at all, so I don't understand where this animation comes from.

When I launch this function :

-(void) moveAlternativeMask:(CALayer*) maskLayer {
     NSLog (@"moveAlternativeMask %@", NSStringFromCGRect(maskLayer.frame));

    // Change the frame of the layer
    CGRect originalFrame = maskLayer.frame;
    CGFloat newYPos = maskLayer.frame.origin.y + 5.;
    maskLayer.frame = CGRectMake (originalFrame.origin.x, newYPos, originalFrame.size.width, originalFrame.size.height);

    // Try removing all animations on the layer
    NSLog (@"Animations");
    for (NSString* animationKey in maskLayer.animationKeys) {
        NSLog(@"    Animation With Key %@", animationKey);
    }
    NSLog (@"End animations");
    [maskLayer removeAllAnimations];

    NSLog (@"Animations after removing");
    for (NSString* animationKey in maskLayer.animationKeys) {
        NSLog(@"    Animation With Key %@", animationKey);
    }
    NSLog (@"End animations after removing");

    // Launch the animation with a small delay
    [self performSelector:@selector(moveAlternativeMask:) withObject:maskLayer afterDelay:0.5];

}

I get this log :

----moveAlternativeMask {{0, 200}, {768, 643}} ---- Animations Animation with key : position End animations Animations after removing
End animations after removing

---moveAlternativeMask {{0, 205}, {768, 643}} Animations Animation with key : position End animations Animations after removing
End animations after removing

---moveAlternativeMask {{0, 210}, {768, 643}} Animations Animation with key : position End animations Animations after removing
End animations after removing

So the layer is moved as I want, the only problem is that the "position" animation keep coming back...

Note that the CALayer was build like this :

UIImage *maskImage = [UIImage imageNamed:@"masque2.png"];
self.maskLayer = [CALayer layer];
self.maskLayer.contents = (id) maskImage.CGImage;
self.maskLayer.frame = CGRectMake(0, ORIGINAL_Y_POSITION, 768, 643);
[myView.layer setMask:self.maskLayer];

Solution

  • Hmm, very strange. I have 2 ideas:

    1. Is it feasible to try doing this with a UIView instead?
    2. Could you perhaps try removeFromSuperview and addSubview instead of just changing the frame?