如何使用角度渐变制作圆形UIView

时间:2016-02-26 17:06:22

标签: ios objective-c core-animation

我需要制作像

这样的视图

enter image description here

这是一个圆形部分,必须有一个角度渐变和圆角。

2 个答案:

答案 0 :(得分:1)

以下是我的工作方式。

  1. 创建CALayer以绘制您的背景'对于装载机。不幸的是,在Core Animation或Core Graphics中没有内置的方法来创建角度渐变。您必须使用带有CAGradientLayer的线性渐变,或者查看AngleGradientLayer

  2. 创建CAShapeLayer以定义圆弧。然后,您需要使用UIBezierPath +bezierPathWithArcCenter:radius:startAngle:endAngle:clockwise:方法创建弧形路径。然后,您可以将此路径分配给CAShapeLayer的{​​{3}}媒体资源。

  3. 然后,您需要配置CAShapeLayer的描边。您需要将path设置为笔划的宽度,并将lineWidth属性设置为kCALineCapRound

  4. CAShapeLayer分配给后台CALayer的{​​{3}}媒体资源。这将掩盖背景'您的装载机到描边路径。

  5. 将您的背景CALayer lineCap添加到UIView' mask。 (或者在视图的as a sublayer方法中创建UIViewCALayer以及layer的子类

  6. 使用CABasicAnimation上的CAShapeLayerreturn your custom layer class属性进行动画制作。

答案 1 :(得分:0)

我使用UIBezierPath类做了类似的事情。特别是addArcWithCenter函数:

let line = UIBezierPath()
line.addArcWithCenter(centerPoint, radius: curveRadius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
color.setStroke()
line.stroke()

centerPoint,curveRadius,startAngle和endAngle都是提前设置的。

颜色是UIColor,用于给出线条颜色。