我可以创建一个这样的面具:
CALayer *mask = [CALayer layer]; mask.contents = (id)[[UIImage imageNamed:@"mask.png"] CGImage]; mask.frame = CGRectMake(0,10,10); self.content.layer.mask = mask;
这将正确显示我的内容的左上角10个像素(因为mask.png只是一个黑色图像).但是,我想为蒙版设置动画以显示其余内容:
[UIView animateWithDuration:3.0 animations:^{ mask.frame = self.content.bounds; } completion:^(BOOL finished){ }];
解决方法
框架是各种其他属性的派生属性,例如position,bounds,anchorPoint以及它应用于它的任何变换.不建议直接为此属性设置动画,尤其是在处理较低级别的CoreAnimation图层时.
在这种情况下,我会假设您想要为边界设置动画.您可以使用上面的UIView动画方法,但在直接处理CALayers时,我更喜欢使用动画的CoreAnimation方法.
CGRect oldBounds = mask.bounds; CGRect newBounds = self.content.bounds; CABasicAnimation* revealAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"]; revealAnimation.fromValue = [NSValue valueWithCGRect:oldBounds]; revealAnimation.toValue = [NSValue valueWithCGRect:newBounds]; revealAnimation.duration = 3.0; // Update the bounds so the layer doesn't snap back when the animation completes. mask.bounds = newBounds; [mask addAnimation:revealAnimation forKey:@"revealAnimation"];