2016-05-27 27 views
-2

我对CALayer不太好,但我需要绘制加号(+),并且我不想使用图像,因为我想为绘图创建动画。任何帮助?使用CALayer绘制加号(+)

+0

创建两个'CAShapeLayer'形成一个加号,问题在哪里?如果您故意让这种情况变得更加困难,您可能需要先自行找到解决方案。 – luk2302

+0

为什么不画两条贝塞尔路径,一条是水平线,一条是垂直线? –

回答

3

经过所有的反对票,我自己就可以做到了。以下是如何为其他人可能需要这个

CGFloat height = 2.f; 
CGFloat width = 3.f; 
CGFloat cornerRadius = 1.f; 

CALayer *hLayer = [CALayer layer];//this is the left - right stroke 
hLayer.frame = CGRectMake(0, CGRectGetMidY(self.bounds)-(height/2), width, height); 
hLayer.cornerRadius = cornerRadius; 

CALayer *vLayer = [CALayer layer];// this is the top - bottom stroke 
vLayer.frame = CGRectMake(CGRectGetMidX(self.bounds) - (height/2), -3,height, width); 
vLayer.cornerRadius = cornerRadius; 

[self.layer addSublayer:hLayer]; 
[self.layer addSublayer:vLayer]; 
2

正如luk2302所说,请使用CAShapeLayer

您可以将CGPath安装到CAShapeLayer中。您可以从任何UIBezierPath获得CGPath。 (它有一个CGPath属性,可以让你得到底层CGPath对象的任何UIBezierPath。)

我建议在UIBezierPath上阅读。它有方法moveToPointaddLineToPoint

您将移动到加号的顶部,向下添加一行,然后移到加号的左侧并添加一行。

请注意,您还可以根据您之后的动画类型制作动画图像。你需要做什么样的动画?

0

另一种简单的方法是使用两个具有相同背景颜色的UIView,并将它们作为子视图添加到另一个UIView。然后,您可以使用UIView animateWithDuration:...方法来完成简单的动画。