1

我想将颜色添加到UIButton CAShape图层。是否可以根据 url 中的参考渐变颜色效果对 CAShapeLayer .. 我们可以将颜色添加到图层。我UIButton CAShape可以看到按钮笔划的颜色,但我想要渐变颜色到按钮CAShape层。请帮助将颜色设置为UIButton CAShape层。代码是

 -(void)changeColour:(id)sender
{

@try{
if(m_cObjbuttonPtr1.layer.sublayers.count > 0){for(CALayer *layer in m_cObjbuttonPtr1.layer.sublayers){if([layer isKindOfClass:[CAShapeLayer class]])              [layer removeFromSuperlayer];}}if (m_cObjbuttonPtr2.layer.sublayers.count > 0){         for(CALayer *layer in m_cObjbuttonPtr2.layer.sublayers){if([layer isKindOfClass:[CAShapeLayer class]])[layer removeFromSuperlayer];}}if(m_cObjbuttonPtr3.layer.sublayers.count > 0){for(CALayer *layer in m_cObjbuttonPtr3.layer.sublayers){if([layer isKindOfClass:[CAShapeLayer class]])                [layer removeFromSuperlayer];}}if (m_cObjbuttonPtr4.layer.sublayers.count > 0){            for(CALayer *layer in m_cObjbuttonPtr4.layer.sublayers){if([layer isKindOfClass:[CAShapeLayer class]])[layer removeFromSuperlayer];}}if(m_cObjbuttonPtr5.layer.sublayers.count > 0){           for(CALayer *layer in m_cObjbuttonPtr5.layer.sublayers){if([layer isKindOfClass:[CAShapeLayer class]])[layer removeFromSuperlayer];}}}@catch (NSException *exception){}
    UIButton *tempButton =(UIButton *)sender;
    UIBezierPath *aPath = [UIBezierPath bezierPath];
    [aPath moveToPoint:CGPointMake(0.0, 0.0)];
    [aPath addLineToPoint:CGPointMake(0.0, 35.0)];
    [aPath moveToPoint:CGPointMake(0.0, 35.0)];
    [aPath addLineToPoint:CGPointMake(80.0, 55.0)];
    [aPath moveToPoint:CGPointMake(80.0, 55.0)];
    [aPath addLineToPoint:CGPointMake(180.0, 35.0)];
    [aPath moveToPoint:CGPointMake(180.0, 35.0)];
    [aPath addLineToPoint:CGPointMake(180.0, 0.0)];
    [aPath moveToPoint:CGPointMake(180.0, 0.0)];
    [aPath addLineToPoint:CGPointMake(0.0, 0.0)];
    [aPath closePath];
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        if(tempButton.tag == 1)
            shapeLayer.frame = m_cObjbuttonPtr1.bounds;
        if(tempButton.tag == 2)
            shapeLayer.frame = m_cObjbuttonPtr2.bounds;
        if(tempButton.tag == 3)
            shapeLayer.frame = m_cObjbuttonPtr3.bounds;
        if(tempButton.tag == 4)
            shapeLayer.frame = m_cObjbuttonPtr4.bounds;
        if(tempButton.tag == 5)
            shapeLayer.frame = m_cObjbuttonPtr5.bounds;
    shapeLayer.path = aPath.CGPath;
    [shapeLayer setStrokeColor:[[UIColor redColor] CGColor]];
    shapeLayer.fillColor = [[UIColor redColor] CGColor];
    shapeLayer.lineWidth = 2;
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    if(tempButton.tag == 1)
        gradientLayer.frame = m_cObjbuttonPtr1.bounds;
    if(tempButton.tag == 2)
        gradientLayer.frame = m_cObjbuttonPtr2.bounds;
    if(tempButton.tag == 3)
        gradientLayer.frame = m_cObjbuttonPtr3.bounds;
    if(tempButton.tag == 4)
        gradientLayer.frame = m_cObjbuttonPtr4.bounds;
    if(tempButton.tag == 5)
        gradientLayer.frame = m_cObjbuttonPtr5.bounds;
    gradientLayer.colors = [NSArray arrayWithObjects:
     (id)[UIColor colorWithRed:0.98f green:0.31f blue:0.46f alpha:0.0].CGColor,
     (id)[UIColor colorWithRed:0.98f green:0.31f blue:0.14f alpha:0.0].CGColor,nil];
    gradientLayer.locations = [NSArray arrayWithObjects:
                               [NSNumber numberWithFloat:0.0f],
                               [NSNumber numberWithFloat:1.0f],
                               nil];
    if(tempButton.tag == 1){
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.startPoint = CGPointMake(0.5,1.0);
        gradientLayer.endPoint = CGPointMake(0.5,0.0);
        gradientLayer.frame = CGRectMake(0, 0, shapeLayer.frame.size.width,                      shapeLayer.frame.size.height);
        NSMutableArray *colors = [NSMutableArray array];
        for (int i = 0; i < 10; i++) {
             [colors addObject:(id)[[UIColor colorWithHue:(0.1 * i) saturation:1       brightness:.8 alpha:1] CGColor]];
        }
        gradientLayer.colors = colors;
        [gradientLayer setMask:shapeLayer];
        [shapeLayer setFillRule:kCAFillRuleEvenOdd];
        [shapeLayer setFillColor:[[UIColor orangeColor] CGColor]];

        [m_cObjbuttonPtr1.layer addSublayer:shapeLayer];
    }
    if(tempButton.tag == 2){

        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.startPoint = CGPointMake(0.5,1.0);
        gradientLayer.endPoint = CGPointMake(0.5,0.0);
        gradientLayer.frame = CGRectMake(0, 0, m_cObjbuttonPtr2.layer.frame.size.width, m_cObjbuttonPtr2.layer.frame.size.height);
        NSMutableArray *colors = [NSMutableArray array];
        for (int i = 0; i < 10; i++) {
            [colors addObject:(id)[[UIColor colorWithHue:(0.1 * i) saturation:1 brightness:.8 alpha:1] CGColor]];
        }
        gradientLayer.colors = [NSArray arrayWithObjects:
            (id)[UIColor colorWithRed:0.98f green:0.31f blue:0.46f alpha:0.0].CGColor,
            (id)[UIColor colorWithRed:0.98f green:0.31f blue:0.14f alpha:0.0].CGColor,
            nil];
        [gradientLayer setMask:shapeLayer];
        [m_cObjbuttonPtr2.layer addSublayer:shapeLayer];
    }
    if(tempButton.tag == 3){
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.startPoint = CGPointMake(0.5,1.0);
        gradientLayer.endPoint = CGPointMake(0.5,0.0);
        gradientLayer.frame = CGRectMake(0, 0, m_cObjbuttonPtr3.layer.frame.size.width, m_cObjbuttonPtr3.layer.frame.size.height);
        NSMutableArray *colors = [NSMutableArray array];
        for (int i = 0; i < 10; i++) {
            [colors addObject:(id)[[UIColor colorWithHue:(0.1 * i) saturation:1 brightness:.8 alpha:1] CGColor]];
        }
        gradientLayer.colors = colors;
        [gradientLayer setMask:shapeLayer];
        [m_cObjbuttonPtr3.layer addSublayer:shapeLayer];
    }
    if(tempButton.tag == 4){
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.startPoint = CGPointMake(0.5,1.0);
        gradientLayer.endPoint = CGPointMake(0.5,0.0);
        gradientLayer.frame = CGRectMake(0, 0, m_cObjbuttonPtr4.layer.frame.size.width, m_cObjbuttonPtr4.layer.frame.size.height);
        NSMutableArray *colors = [NSMutableArray array];
        for (int i = 0; i < 10; i++) {
            [colors addObject:(id)[[UIColor colorWithHue:(0.1 * i) saturation:1 brightness:.8 alpha:1] CGColor]];
        }
        gradientLayer.colors = colors;
        [gradientLayer setMask:shapeLayer];
        [m_cObjbuttonPtr4.layer addSublayer:shapeLayer];
    }
    if(tempButton.tag == 5){
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.startPoint = CGPointMake(0.5,1.0);
        gradientLayer.endPoint = CGPointMake(0.5,0.0);
        gradientLayer.frame = CGRectMake(0, 0, m_cObjbuttonPtr5.layer.frame.size.width, m_cObjbuttonPtr5.layer.frame.size.height);
        NSMutableArray *colors = [NSMutableArray array];
        for (int i = 0; i < 10; i++) {
            [colors addObject:(id)[[UIColor colorWithHue:(0.1 * i) saturation:1 brightness:.8 alpha:1] CGColor]];
        }
        gradientLayer.colors = colors;
        [gradientLayer setMask:shapeLayer];
        [m_cObjbuttonPtr5.layer addSublayer:shapeLayer];
    }

}
4

0 回答 0