objective-c - 使用 mask 在 CALayer 上遮挡动画

标签 objective-c core-animation uiimage calayer mask

我想在图像上创建“遮蔽”效果,以便图像“遮蔽”并出现。

有点像这个 JavaScript 转换: https://github.com/madrobby/scriptaculous - http://madrobby.github.io/scriptaculous/effect-blinddown/

蒙版设置正确,因为如果我手动更改它的位置,它会隐藏和显示它后面的图像,但它不会动画!它只是在动画的最终位置结束,你永远看不到它真正的盲目。

求助!也许这不是实现盲目效果的最佳方式?

// create a new layer
CALayer *numberLayer = [[CALayer alloc] init];

// render the number "7" on the layer
UIImage *image = [UIImage imageNamed:@"number-7.png"];
numberLayer.contents = (id) [image CGImage];
numberLayer.bounds = CGRectMake(0, 0, image.size.width, image.size.height); // width and height are 50
numberLayer.position = position;

// create a new mask that is 50x50 the size of the image
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
CGMutablePathRef path = CGPathCreateMutable();

CGPathAddRect(path, nil, CGRectMake(0, 0, 50, 50));

[maskLayer setPath:path];
[maskLayer setFillColor:[[UIColor whiteColor] CGColor]];

[theLayer setMask:maskLayer];

[maskLayer setPosition:CGPointMake(0, 0)]; // place the mask over the image

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:3.0];
[maskLayer setPosition:CGPointMake(0, 50)]; // slide mask off the image
// this should shift the blind away in an animation
// but it doesn't animate
[UIView commitAnimations]; 
[maskLayer release];

[boardLayer addSublayer:numberLayer];
[numberLayer release];

最佳答案

另一个尝试。查看 CALayercontentRect 属性。它充当图层上的“视口(viewport)”。要为其设置动画,请使用缩小的值启动 contentRect,然后将其设置为全尺寸。 Core Animation 负责为您制作动画。

不确定这是否正是您要寻找的效果,但有几点需要牢记:

  • contentsRect 位于“单位坐标”中,表示值是介于 0.0 和 1.0 之间的 float (0.0 表示什么都不显示,1.0 表示显示所有内容)。

  • 图层坐标从 UIView 坐标翻转过来。所以 0.0、0.0 是左下角,值向上和向右。您必须进行一些调整才能获得正确的效果。

  • 根据百叶窗内的内容,您可能必须设置 masksToBounds=YES

关于objective-c - 使用 mask 在 CALayer 上遮挡动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2385376/

相关文章:

ios - NSMutableDictionary 没有写入文件

objective-c - 为什么边框粗细会随着按钮的颜色而变化(蓝色边框为 2px)?

objective-c - removeAllAnimations 不适用于连续调用的动画

ios - 核心动画 : Flipp a view

ios - 使用 glob 获取目录中的文件列表

ios - 如何为一对多关系 Core Data iOS 添加谓词

ios - SceneKit Rigged 角色动画提高性能

ios - 如何在质量损失最小的情况下通过网络传输小型 UIImages?

ios - 如何仅在代码中打开 ipad 上的图像选择器

iphone - UIImage 中的一个圆圈