0

I have created 3 custom views in a UIViewController. In each custom view, I have created shapes using CAShapeLayer.

These layers also contain gradient colour. Now I want to set gradient colour to custom views. When I am trying to do that, it is crashing. For first view, here is code :

//first component
    self.aTimeScaleMonthView = [[TimeScaleView alloc] initWithFrame:CGRectMake(ORIGIN_X, frame.origin.y, frame.size.width-(2*ORIGIN_X), HEIGHT_OF_COMPONENT1) withStartDate:startDate endDate:endDate];
    self.aTimeScaleMonthView.modeOfScale = A3TimeScaleMonth;
    self.aTimeScaleMonthView.layer.borderColor = [UIColor blackColor].CGColor;
    self.aTimeScaleMonthView.layer.borderWidth = BORDER_WIDTH_BOX;

    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = self.aTimeScaleMonthView.bounds;
    gradient.colors = [NSArray arrayWithObjects:[UIColor colorWithRed:0.66 green:0.29 blue:0.22 alpha:1.0], [UIColor colorWithRed:0.62 green:0.51 blue:0.314 alpha:1.0], nil];

    [self.aTimeScaleMonthView.layer insertSublayer:gradient atIndex:0];
    [self addSubview: self.aTimeScaleMonthView];

Please help me.

geo
  • 1,781
  • 1
  • 18
  • 30
user007
  • 69
  • 8

2 Answers2

2

Gradient colors should be CGColor: gradient.colors = [NSArray arrayWithObjects:(id)[UIColor colorWithRed:0.66 green:0.29 blue:0.22 alpha:1.0].CGColor, (id)[UIColor colorWithRed:0.62 green:0.51 blue:0.314 alpha:1.0].CGColor, nil];

Btw, you forgot to set start and end points for your gradient.

Kyr Dunenkoff
  • 8,090
  • 3
  • 23
  • 21
0

CALayer and its subclasses take CGColor, not UIColor. You can transmute a UIColor to the Core Graphics color struct CGColor by accessing the CGColor property of UIColor instances.

Also important to understand: CoreFoundation structures & those of related C APIs such as CoreGraphics (anything starting with CG is CoreGraphics related) cannot be inserted into NSArray straight out because they are not object-oriented. You must cast the structure to id to place it inside an NSArray instance. You cast in Objective-C using parens around the type you wish to cast to. In this case you would do the following:

gradient.colors = @[(id)[UIColor colorWithRed:0.66 green:0.29 blue:0.22 alpha:1.0].CGColor, (id)[UIColor colorWithRed:0.62 green:0.51 blue:0.314 alpha:1.0].CGColor];

To make your code more readable and your colors reusable, you should assign the colors to descriptive variables and add those variables into the NSArray in their place.

You do not need to set the startPoint and endPoint if a straight up and down gradient is what you want. That is the most common desired configuration and is accordingly the default. Also, using object literal syntax for array creation is preferred today over @selector(arrayWithObjects:).

Don't forget to set the layerClass class method of your custom UIView subclass.

james_womack
  • 10,028
  • 6
  • 55
  • 74
  • Whomever downvoted me without a comment (especially given my answer correct and highly detailed)—that's rude and not the way to behave on Stack Overflow. No one learns from a downvote without a descriptive comment saying what the downvote is for. – james_womack Jul 09 '13 at 08:15