简体   繁体   中英

Change CGRect or Override CGRect

I am drawing a radial gradient in quartz. I created a new file and put the code in drawRect as

CGContextRef ctx = UIGraphicsGetCurrentContext();

//Draw the gray Gradient

CGFloat BGLocations[2] = { 0.0, 1.0 };
CGFloat BgComponents[8] = { 0.25, 0.25, 0.25 , 1.0,  // Start color
   0.11, 0.11, 0.11, 1.0 }; // Mid color and End color
CGColorSpaceRef BgRGBColorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef bgRadialGradient = CGGradientCreateWithColorComponents(BgRGBColorspace, BgComponents, BGLocations, 2);


CGPoint startBg = CGPointMake(250, 250); 
CGFloat endRadius= 250;


CGContextDrawRadialGradient(ctx, bgRadialGradient, startBg, 0, startBg, endRadius, kCGGradientDrawsAfterEndLocation);
CGColorSpaceRelease(BgRGBColorspace);
CGGradientRelease(bgRadialGradient);

Now i am making a UIView and initializing it with the RadialGradientView created through the website but i need to change the endRadius according to the different implementations.

IS there a way to do that.

I am initializing the UIView with Frame.

UIView* backgroundView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];

    UIView* gradientView = [[RadialGradientView alloc] initWithFrame: backgroundView.bounds];

So when i am trying to access endRadius property of gradientView, Its not showing up.

Declare the endRadius as a property in your subclassed yourView.h file as

@property(assign) CGFloat endRadius;

and you can access this property like:

   yourView.endRadius=40;

and in the code , change this line:

CGContextDrawRadialGradient(ctx, bgRadialGradient, startBg, 0, startBg, endRadius, kCGGradientDrawsAfterEndLocation);

to

CGContextDrawRadialGradient(ctx, bgRadialGradient, startBg, 0, startBg, self.endRadius, kCGGradientDrawsAfterEndLocation);

or

CGContextDrawRadialGradient(ctx, bgRadialGradient, startBg, 0, startBg, _endRadius, kCGGradientDrawsAfterEndLocation);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM