如何改变CAGradientLayer色点? [英] How to change CAGradientLayer color points?

查看:119
本文介绍了如何改变CAGradientLayer色点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有一个GA Gradient图层,我设置了颜色,但我想将颜色点设置为(而不是顶部中心,左上角)和底部(而不是底部中心)到右下角)只是为了改变一点。思考?下面是我到目前为止的代码......我包含核心动画,因为我是颜色之间的动画。

Right now I've got a GA Gradient layer where I've set the colors but I'd like to set the color point to (instead of top center, to top left) and the bottom to (instead of bottom center to bottom right) just to change things up a bit. Thoughts? Below is the code that I've got so far...I included core animation because I'm animation between colors.

- (id)init {
    self = [super init];

    UIView *gradientView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.w, self.h)];
    [self addSubview:gradientView];
    [self sendSubviewToBack:gradientView];

    topColor = [UIColor colorWithRed:0.012 green:0.012 blue:0.012 alpha:1];
    bottomColor = [UIColor colorWithRed:1.000 green:0.765 blue:0.235 alpha:1];

    gradient = [CAGradientLayer layer];
    gradient.frame = gradientView.frame;
    gradient.colors = [NSArray arrayWithObjects:(id)topColor.CGColor, (id)bottomColor.CGColor, nil];
    gradient.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:0.7], nil];
    [gradientView.layer addSublayer:gradient];

    [self performSelector:@selector(animateColors) withObject:self afterDelay:2.0];
    currentColorCount = 1;
    return self;
}

在左边(我有什么)左边(我是什么) 'd like)

On the right (What I've got) on the left (what I'd like)

推荐答案

startPoint endPoint CAGradientLayer 的属性在单位坐标系中定义。在单位坐标系中:

The startPoint and endPoint properties of a CAGradientLayer are defined in the "unit coordinate system". In the unit coordinate system:


  • (0,0)对应最小的图层边界矩形的坐标,在iOS上是左上角,除非图层已经变换;

  • (1,1)对应于图层边界矩形的最大坐标,除非图层已经变换,否则在iOS上是右下角。

  • (0,0) corresponds to the smallest coordinates of the layer's bounds rectangle, which on iOS is its upper-left corner unless the layer has been transformed;
  • (1,1) corresponds to the largest coordinates of the layer's bounds rectangle, which on iOS is its lower-right corner unless the layer has been transformed.

因此按照你想要的方式安排你的渐变应该很简单:

Thus arranging your gradient the way you want should be this simple:

gradient.startPoint = CGPointZero;
gradient.endPoint = CGPointMake(1, 1);

这篇关于如何改变CAGradientLayer色点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆