【发布时间】:2013-06-12 20:48:50
【问题描述】:
我一直在尝试了解更多关于使用CAShapeLayer 使用路径对其进行动画处理的信息。
我想到的第一件事是在弹开之前对边缘产生“压缩”网球的效果。
但我没有使用图层的fillPath,而是尝试为该图层设置图像。
所以我创建了另一个CALayer,分配给它的内容属性一个图像并动画形状层的路径。
但结果是,图像并没有完全包裹在这条路径中,正如我希望的那样,它会随着路径的动画而增长和缩小,而是在路径动画的同时显示和覆盖图像。
我希望能够让这个图像完全被路径包裹起来,并且当路径被动画化时,无论形状如何,图像仍然被包裹在形状层上的这个路径中。图像失真在这里不是问题。
我的想法是,例如,有一个我下载的网球图像,将其包裹在 CAShapeLayer 中,以倒置的泪珠状路径(见图:)并从较小的动画倒置的泪珠形状变成更大的倒置泪珠,最终显示为圆形(网球图像)。
是否可以使用CAShapeLayer 来实现这一点?你能在这里给我一个样品吗?提前致谢。
添加代码:
嗨,这是我目前得到的(感谢@lnafziger)
//Setting up layer
- (void)setupLayer
{
self.caShapeLayer = [CAShapeLayer layer];<
self.caLayer = (CALayer*) self.layer;
self.caLayer.contents = (id)([UIImage imageNamed:"@yellowTennisBall.png"] CGImage]);
self.caLayer.mask = self.caShapeLayer;
}
//Animate the path of CAShapeLayer
- (void)bounce
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
animation.duration = 1.7;
animation.repeatCount = HUGE_VAL;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.fromValue = (__bridge id)[self defaultPath];
animation.toValue = (__bridge id)[self compressedPath];
animation.autoreverses = YES;
[self.caShapeLayer addAnimation:animation forKey:@"animatePath"];
}
对于前面提到的效果以及将 UIImage 包装在路径中,我在编码方面一无所获……
【问题讨论】:
-
将路径层设置为图像层的
maskLayer应该可以满足您的需求。 (您可能需要为描边效果使用额外的路径层)。 -
Core Animation 不容易做到这一点。将图像变形为任意路径是一件相当复杂的事情,并且没有公共 API 可以帮助您。
标签: ios cashapelayer