0

我想CAGradientLayer为不同大小的多个视图创建一个颜色渐变。我不知道如何单独定义框架:

UIColor *darkOp = [UIColor colorWithRed:0.2f green:0.2f blue:0.27f alpha:1.0];
UIColor *lightOp = [UIColor colorWithRed:0.36f green:0.35f blue:0.42f alpha:1.0];

// Create the gradient
CAGradientLayer *gradient = [CAGradientLayer layer];

// Set colors
gradient.colors = [NSArray arrayWithObjects:
                   (id)darkOp.CGColor,
                   (id)lightOp.CGColor,
                   nil];
//set radius
gradient.cornerRadius = 5.0;

// Set bounds BUT just for one view size
gradient.frame = self.numberRegionView.bounds; //<-- here I can just define one frame size

// Add the gradient to one view
[self.numberRegionView.layer insertSublayer:gradient atIndex:0];

//but how to add the gradient layer to views with different sizes ???
//[self.graphRegionView.layer insertSublayer:gradient atIndex:0]; ???
//[self.barRegionView.layer insertSublayer:gradient atIndex:0];   ???

谢谢!

4

1 回答 1

1
-(void)setGradientForView:(UIView*)view
{
    static UIColor *darkOp = [UIColor colorWithRed:0.2f green:0.2f blue:0.27f alpha:1.0];
    static UIColor *lightOp = [UIColor colorWithRed:0.36f green:0.35f blue:0.42f alpha:1.0];

    // Create the gradient
    CAGradientLayer *gradient = [CAGradientLayer layer];

    // Set colors
    gradient.colors = [NSArray arrayWithObjects:
                   (id)darkOp.CGColor,
                   (id)lightOp.CGColor,
                   nil];
    //set radius
    gradient.cornerRadius = 5.0;

    // Set bounds BUT just for one view size
    gradient.frame = view.bounds; //<-- here I can just define one frame size

    // Add the gradient to one view
    [view.layer insertSublayer:gradient atIndex:0];
}

然后将此代码用于您的三个视图:

[self setGradientForView:self.numberRegionView];
[self setGradientForView:self.barRegionView];
[self setGradientForView:self.numberRegionView];
于 2014-01-23T21:56:40.843 回答