Ios CAShapeLayer与UIImage
我一直在努力学习更多关于使用Ios CAShapeLayer与UIImage,ios,cashapelayer,Ios,Cashapelayer,我一直在努力学习更多关于使用CAShapeLayer使用path设置动画的知识。 我想到的第一件事是,在网球反弹之前,它会有一个“压缩”的网球撞击边缘的效果 但是,我没有使用该层的fillPath,而是尝试为该层设置一个图像。 因此,我创建了另一个CALayer,为其内容属性指定了一个图像,并设置了形状层路径的动画 但结果是,图像不是完全包裹在这条路径中,正如我所希望的那样,随着路径的动画化,图像会增长和收缩,而是在路径动画化时,图像被显示和覆盖 我希望能够有这个图像完全包装的路径,因为路径是动
CAShapeLayer
使用path设置动画的知识。
我想到的第一件事是,在网球反弹之前,它会有一个“压缩”的网球撞击边缘的效果
但是,我没有使用该层的fillPath
,而是尝试为该层设置一个图像。
因此,我创建了另一个CALayer
,为其内容属性指定了一个图像,并设置了形状层路径的动画
但结果是,图像不是完全包裹在这条路径中,正如我所希望的那样,随着路径的动画化,图像会增长和收缩,而是在路径动画化时,图像被显示和覆盖
我希望能够有这个图像完全包装的路径,因为路径是动画,无论形状,图像仍然是包装在这个路径上的形状层。图像失真在这里不是问题
例如,我想到的是,我下载了一个网球图像,用CAShapeLayer
将其包裹在一个像泪滴一样的倒置路径中(见图:),并将较小的倒置泪滴形状动画化为较大的倒置泪滴,最后显示一个圆(一个网球图像)
是否可以使用CAShapeLayer
?你能给我一个样品吗?提前谢谢
添加代码:
嗨,这是我到目前为止得到的(感谢@lnafziger)
//设置层
-(无效)设置层
{
self.caShapeLayer=[caShapeLayer层]<
self.caLayer=(caLayer*)self.layer;
self.caLayer.contents=(id)([UIImage ImageName:@yellowTennisBall.png”]cImage]);
self.caLayer.mask=self.caShapeLayer;
}
//设置CAShapeLayer路径的动画
-(无效)反弹
{
cabasicanition*animation=[cabasicanition animationWithKeyPath:@“path”];
animation.duration=1.7;
animation.repeatCount=巨大的数值;
animation.timingFunction=[CamediaTimingFunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.fromValue=(_桥id)[self-defaultPath];
animation.toValue=(_桥id)[自压缩路径];
animation.autoreverses=是;
[self.caShapeLayer addAnimation:animation forKey:@“animatePath”];
}
对于上面提到的效果和在路径中包装UIImage,我在编码方面一无所获 CAShapeLayer很适合掩蔽,但你的需求不仅仅是掩蔽。您需要对图像进行变形,但这并不简单,而且可能无法通过层实现 有一些选择 首先(最难的一个):分别通过代码将你的标记画到黑点。然后通过一个简单的CoreAnimation设置更改的动画 第二个(简单的一个):创建一系列图像,然后像这样开始动画:
NSArray * imageArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"6.png"],
[UIImage imageNamed:@"7.png"],
[UIImage imageNamed:@"8.png"],
[UIImage imageNamed:@"9.png"],
[UIImage imageNamed:@"10.png"],
[UIImage imageNamed:@"11.png"],
[UIImage imageNamed:@"12.png"],
nil];
UIImageView * theImageViewToAnimate = [[UIImageView alloc] initWithFrame:
CGRectMake(100, 125, 150, 130)];
theImageViewToAnimate.animationImages = imageArray;
theImageViewToAnimate.animationDuration = 1.1;
theImageViewToAnimate.animationRepeatCount=1;
[theImageViewToAnimate startAnimating];
将路径层设置为图像层的
maskLayer
应该可以实现您想要的功能。(您可能希望为笔划效果使用附加的路径层)。这在核心动画中不容易实现。将图像变形为任意路径是一件相当复杂的事情,没有公共API可以帮助您实现这一点。在这里绘图是一项艰巨的工作。。您需要在代码中指定硬边界,如果矩形与边界相交,则相应地绘制它们。。所以或多或少是一个假的物理模拟。我宁愿使用图像阵列或直接使用物理引擎
NSArray * imageArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
[UIImage imageNamed:@"6.png"],
[UIImage imageNamed:@"7.png"],
[UIImage imageNamed:@"8.png"],
[UIImage imageNamed:@"9.png"],
[UIImage imageNamed:@"10.png"],
[UIImage imageNamed:@"11.png"],
[UIImage imageNamed:@"12.png"],
nil];
UIImageView * theImageViewToAnimate = [[UIImageView alloc] initWithFrame:
CGRectMake(100, 125, 150, 130)];
theImageViewToAnimate.animationImages = imageArray;
theImageViewToAnimate.animationDuration = 1.1;
theImageViewToAnimate.animationRepeatCount=1;
[theImageViewToAnimate startAnimating];