CAGradientLayer - 渐变颜色(iOS)
博客专区 > hell03W 的博客 > 博客详情
CAGradientLayer - 渐变颜色(iOS)
hell03W 发表于1年前
CAGradientLayer - 渐变颜色(iOS)
  • 发表于 1年前
  • 阅读 40
  • 收藏 1
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

CAGradientLayer继承自CALayer, 是 Core Animation 中的一个, 主要功能是用来时间渐变颜色图层.

效果如下所示:

代码实现:

    // 添加渐变色层
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.bounds;

    gradientLayer.colors = [NSArray arrayWithObjects:
                            (id)[kColorFromRGB(0xF6C48E) CGColor],
                            (id)[kColorFromRGB(0xBB7FCB) CGColor],
                            (id)[kColorFromRGB(0x6DD2E5) CGColor], nil];
    gradientLayer.startPoint = CGPointMake(0, 0.4);
    gradientLayer.endPoint = CGPointMake(1, 0.6);
    _gradientLayer = gradientLayer;

    [self.layer insertSublayer:gradientLayer atIndex:0];

这样的功能, 实现起来还是比较简单的, 苹果给我们全部都封装好了, 只需要调用几个简单的方法即可.

事实上, CAGradientLayer类也是比较简单的, 只提供如下几个方法和属性, 好吧, 当了解之后, 觉得太简单了, 没什么好说的了 ....

@interface CAGradientLayer : CALayer

/* The array of CGColorRef objects defining the color of each gradient
 * stop. Defaults to nil. Animatable. */

@property(nullable, copy) NSArray *colors;

/* An optional array of NSNumber objects defining the location of each
 * gradient stop as a value in the range [0,1]. The values must be
 * monotonically increasing. If a nil array is given, the stops are
 * assumed to spread uniformly across the [0,1] range. When rendered,
 * the colors are mapped to the output colorspace before being
 * interpolated. Defaults to nil. Animatable. */

@property(nullable, copy) NSArray<NSNumber *> *locations;

/* The start and end points of the gradient when drawn into the layer's
 * coordinate space. The start point corresponds to the first gradient
 * stop, the end point to the last gradient stop. Both points are
 * defined in a unit coordinate space that is then mapped to the
 * layer's bounds rectangle when drawn. (I.e. [0,0] is the bottom-left
 * corner of the layer, [1,1] is the top-right corner.) The default values
 * are [.5,0] and [.5,1] respectively. Both are animatable. */

@property CGPoint startPoint;
@property CGPoint endPoint;

/* The kind of gradient that will be drawn. Currently the only allowed
 * value is `axial' (the default value). */

@property(copy) NSString *type;

@end

共有 人打赏支持
粉丝 8
博文 130
码字总数 95347
×
hell03W
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: