How to make radial gradient to be faded in the middle of view in Objective C
I want to make radial gradient view as above attached image but I am not able to make fade in the middle, here is my code for gradient.
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = view_background.bounds;
gradient.colors = @[(id)[UIColor colorWithRed:0.93 green:0.95 blue:0.22 alpha:1].CGColor, (id)[UIColor colorWithRed:0.08 green:0.42 blue:0.32 alpha:1].CGColor];
gradient.endPoint = CGPointMake(0.0, 0.5);
gradient.startPoint = CGPointMake(0.5, 1.0);
[view_background.layer insertSublayer: gradient atIndex:0];
ios objective-c gradient cagradientlayer
add a comment |
I want to make radial gradient view as above attached image but I am not able to make fade in the middle, here is my code for gradient.
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = view_background.bounds;
gradient.colors = @[(id)[UIColor colorWithRed:0.93 green:0.95 blue:0.22 alpha:1].CGColor, (id)[UIColor colorWithRed:0.08 green:0.42 blue:0.32 alpha:1].CGColor];
gradient.endPoint = CGPointMake(0.0, 0.5);
gradient.startPoint = CGPointMake(0.5, 1.0);
[view_background.layer insertSublayer: gradient atIndex:0];
ios objective-c gradient cagradientlayer
Cannot validate it (hence a comment and not an answer) but I guess you have to setgradient.locations = @[@0, @0.5, @1.0];
. You can remove start and end points (the default values are left-middle, right-middle which is what you need). As a sidenote, the image posted seems to be a radial gradient, with the center of the yellowish circle at middle-top.
– Alladinian
Nov 22 '18 at 12:00
i did as you said but gradient.locations = @[@0, @0.5, @1.0]; makes vertical gradient
– Jack
Nov 22 '18 at 13:23
add a comment |
I want to make radial gradient view as above attached image but I am not able to make fade in the middle, here is my code for gradient.
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = view_background.bounds;
gradient.colors = @[(id)[UIColor colorWithRed:0.93 green:0.95 blue:0.22 alpha:1].CGColor, (id)[UIColor colorWithRed:0.08 green:0.42 blue:0.32 alpha:1].CGColor];
gradient.endPoint = CGPointMake(0.0, 0.5);
gradient.startPoint = CGPointMake(0.5, 1.0);
[view_background.layer insertSublayer: gradient atIndex:0];
ios objective-c gradient cagradientlayer
I want to make radial gradient view as above attached image but I am not able to make fade in the middle, here is my code for gradient.
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = view_background.bounds;
gradient.colors = @[(id)[UIColor colorWithRed:0.93 green:0.95 blue:0.22 alpha:1].CGColor, (id)[UIColor colorWithRed:0.08 green:0.42 blue:0.32 alpha:1].CGColor];
gradient.endPoint = CGPointMake(0.0, 0.5);
gradient.startPoint = CGPointMake(0.5, 1.0);
[view_background.layer insertSublayer: gradient atIndex:0];
ios objective-c gradient cagradientlayer
ios objective-c gradient cagradientlayer
edited Nov 23 '18 at 5:46
Sateesh
2,1171819
2,1171819
asked Nov 22 '18 at 7:24


JackJack
536
536
Cannot validate it (hence a comment and not an answer) but I guess you have to setgradient.locations = @[@0, @0.5, @1.0];
. You can remove start and end points (the default values are left-middle, right-middle which is what you need). As a sidenote, the image posted seems to be a radial gradient, with the center of the yellowish circle at middle-top.
– Alladinian
Nov 22 '18 at 12:00
i did as you said but gradient.locations = @[@0, @0.5, @1.0]; makes vertical gradient
– Jack
Nov 22 '18 at 13:23
add a comment |
Cannot validate it (hence a comment and not an answer) but I guess you have to setgradient.locations = @[@0, @0.5, @1.0];
. You can remove start and end points (the default values are left-middle, right-middle which is what you need). As a sidenote, the image posted seems to be a radial gradient, with the center of the yellowish circle at middle-top.
– Alladinian
Nov 22 '18 at 12:00
i did as you said but gradient.locations = @[@0, @0.5, @1.0]; makes vertical gradient
– Jack
Nov 22 '18 at 13:23
Cannot validate it (hence a comment and not an answer) but I guess you have to set
gradient.locations = @[@0, @0.5, @1.0];
. You can remove start and end points (the default values are left-middle, right-middle which is what you need). As a sidenote, the image posted seems to be a radial gradient, with the center of the yellowish circle at middle-top.– Alladinian
Nov 22 '18 at 12:00
Cannot validate it (hence a comment and not an answer) but I guess you have to set
gradient.locations = @[@0, @0.5, @1.0];
. You can remove start and end points (the default values are left-middle, right-middle which is what you need). As a sidenote, the image posted seems to be a radial gradient, with the center of the yellowish circle at middle-top.– Alladinian
Nov 22 '18 at 12:00
i did as you said but gradient.locations = @[@0, @0.5, @1.0]; makes vertical gradient
– Jack
Nov 22 '18 at 13:23
i did as you said but gradient.locations = @[@0, @0.5, @1.0]; makes vertical gradient
– Jack
Nov 22 '18 at 13:23
add a comment |
1 Answer
1
active
oldest
votes
Try the code below. Make RadialGradientView
as IBDesignable
.
RadialGradientView.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
IB_DESIGNABLE
@interface RadialGradientView : UIView
@end
NS_ASSUME_NONNULL_END
RadialGradientView.m
#import "RadialGradientView.h"
#import <CoreGraphics/CoreGraphics.h>
@implementation RadialGradientView
- (void)drawRect:(CGRect)rect {
// Drawing code
UIColor* radialColor = [[UIColor colorWithRed:0.93 green:0.95 blue:0.22 alpha:1] CGColor];
UIColor* outerColor = [[UIColor colorWithRed:0.08 green:0.42 blue:0.32 alpha:1] CGColor];
[self.layer setCornerRadius: 10.0];
[self.layer setMasksToBounds:YES];
NSArray* colors = [[NSArray alloc] initWithObjects: radialColor, outerColor, nil];
CGFloat endRadius = sqrt(pow(self.frame.size.width / 1.9, 2) + pow(self.frame.size.height / 1.9, 2));
CGPoint startCenter = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 12);
CGPoint center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
CGGradientRef gradient = CGGradientCreateWithColors(nil, (CFArrayRef)colors, nil);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawRadialGradient(context, gradient, startCenter, 0.0, center, endRadius, 0);
}
@end
Swift
@IBDesignable class RadialGradientView: UIView {
@IBInspectable var radialColor: UIColor = UIColor(red: 0.93, green: 0.95, blue: 0.22, alpha: 1.0)
@IBInspectable var outerColor: UIColor = UIColor(red: 0.08, green: 0.42, blue: 0.32, alpha: 1.0)
override func draw(_ rect: CGRect) {
layer.cornerRadius = 10.0
layer.masksToBounds = true
let colors = [radialColor.cgColor, outerColor.cgColor]
let endRadius = sqrt(pow(frame.width/1.9, 2) + pow(frame.height/1.9, 2))
let startCenter = CGPoint(x: bounds.size.width / 2, y: bounds.size.height / 12)
let center = CGPoint(x: bounds.size.width / 2, y: bounds.size.height / 2)
let gradient = CGGradient(colorsSpace: nil, colors: colors as CFArray, locations: nil)
let context = UIGraphicsGetCurrentContext()
context?.drawRadialGradient(gradient!, startCenter: startCenter, startRadius: 0.0, endCenter: center, endRadius: endRadius, options: CGGradientDrawingOptions.drawsBeforeStartLocation)
}
}
Screenshot
thanks worked nice)
– Jack
Nov 22 '18 at 13:53
Glad to help you Jack 😀
– Sateesh
Nov 22 '18 at 13:57
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425773%2fhow-to-make-radial-gradient-to-be-faded-in-the-middle-of-view-in-objective-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try the code below. Make RadialGradientView
as IBDesignable
.
RadialGradientView.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
IB_DESIGNABLE
@interface RadialGradientView : UIView
@end
NS_ASSUME_NONNULL_END
RadialGradientView.m
#import "RadialGradientView.h"
#import <CoreGraphics/CoreGraphics.h>
@implementation RadialGradientView
- (void)drawRect:(CGRect)rect {
// Drawing code
UIColor* radialColor = [[UIColor colorWithRed:0.93 green:0.95 blue:0.22 alpha:1] CGColor];
UIColor* outerColor = [[UIColor colorWithRed:0.08 green:0.42 blue:0.32 alpha:1] CGColor];
[self.layer setCornerRadius: 10.0];
[self.layer setMasksToBounds:YES];
NSArray* colors = [[NSArray alloc] initWithObjects: radialColor, outerColor, nil];
CGFloat endRadius = sqrt(pow(self.frame.size.width / 1.9, 2) + pow(self.frame.size.height / 1.9, 2));
CGPoint startCenter = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 12);
CGPoint center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
CGGradientRef gradient = CGGradientCreateWithColors(nil, (CFArrayRef)colors, nil);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawRadialGradient(context, gradient, startCenter, 0.0, center, endRadius, 0);
}
@end
Swift
@IBDesignable class RadialGradientView: UIView {
@IBInspectable var radialColor: UIColor = UIColor(red: 0.93, green: 0.95, blue: 0.22, alpha: 1.0)
@IBInspectable var outerColor: UIColor = UIColor(red: 0.08, green: 0.42, blue: 0.32, alpha: 1.0)
override func draw(_ rect: CGRect) {
layer.cornerRadius = 10.0
layer.masksToBounds = true
let colors = [radialColor.cgColor, outerColor.cgColor]
let endRadius = sqrt(pow(frame.width/1.9, 2) + pow(frame.height/1.9, 2))
let startCenter = CGPoint(x: bounds.size.width / 2, y: bounds.size.height / 12)
let center = CGPoint(x: bounds.size.width / 2, y: bounds.size.height / 2)
let gradient = CGGradient(colorsSpace: nil, colors: colors as CFArray, locations: nil)
let context = UIGraphicsGetCurrentContext()
context?.drawRadialGradient(gradient!, startCenter: startCenter, startRadius: 0.0, endCenter: center, endRadius: endRadius, options: CGGradientDrawingOptions.drawsBeforeStartLocation)
}
}
Screenshot
thanks worked nice)
– Jack
Nov 22 '18 at 13:53
Glad to help you Jack 😀
– Sateesh
Nov 22 '18 at 13:57
add a comment |
Try the code below. Make RadialGradientView
as IBDesignable
.
RadialGradientView.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
IB_DESIGNABLE
@interface RadialGradientView : UIView
@end
NS_ASSUME_NONNULL_END
RadialGradientView.m
#import "RadialGradientView.h"
#import <CoreGraphics/CoreGraphics.h>
@implementation RadialGradientView
- (void)drawRect:(CGRect)rect {
// Drawing code
UIColor* radialColor = [[UIColor colorWithRed:0.93 green:0.95 blue:0.22 alpha:1] CGColor];
UIColor* outerColor = [[UIColor colorWithRed:0.08 green:0.42 blue:0.32 alpha:1] CGColor];
[self.layer setCornerRadius: 10.0];
[self.layer setMasksToBounds:YES];
NSArray* colors = [[NSArray alloc] initWithObjects: radialColor, outerColor, nil];
CGFloat endRadius = sqrt(pow(self.frame.size.width / 1.9, 2) + pow(self.frame.size.height / 1.9, 2));
CGPoint startCenter = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 12);
CGPoint center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
CGGradientRef gradient = CGGradientCreateWithColors(nil, (CFArrayRef)colors, nil);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawRadialGradient(context, gradient, startCenter, 0.0, center, endRadius, 0);
}
@end
Swift
@IBDesignable class RadialGradientView: UIView {
@IBInspectable var radialColor: UIColor = UIColor(red: 0.93, green: 0.95, blue: 0.22, alpha: 1.0)
@IBInspectable var outerColor: UIColor = UIColor(red: 0.08, green: 0.42, blue: 0.32, alpha: 1.0)
override func draw(_ rect: CGRect) {
layer.cornerRadius = 10.0
layer.masksToBounds = true
let colors = [radialColor.cgColor, outerColor.cgColor]
let endRadius = sqrt(pow(frame.width/1.9, 2) + pow(frame.height/1.9, 2))
let startCenter = CGPoint(x: bounds.size.width / 2, y: bounds.size.height / 12)
let center = CGPoint(x: bounds.size.width / 2, y: bounds.size.height / 2)
let gradient = CGGradient(colorsSpace: nil, colors: colors as CFArray, locations: nil)
let context = UIGraphicsGetCurrentContext()
context?.drawRadialGradient(gradient!, startCenter: startCenter, startRadius: 0.0, endCenter: center, endRadius: endRadius, options: CGGradientDrawingOptions.drawsBeforeStartLocation)
}
}
Screenshot
thanks worked nice)
– Jack
Nov 22 '18 at 13:53
Glad to help you Jack 😀
– Sateesh
Nov 22 '18 at 13:57
add a comment |
Try the code below. Make RadialGradientView
as IBDesignable
.
RadialGradientView.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
IB_DESIGNABLE
@interface RadialGradientView : UIView
@end
NS_ASSUME_NONNULL_END
RadialGradientView.m
#import "RadialGradientView.h"
#import <CoreGraphics/CoreGraphics.h>
@implementation RadialGradientView
- (void)drawRect:(CGRect)rect {
// Drawing code
UIColor* radialColor = [[UIColor colorWithRed:0.93 green:0.95 blue:0.22 alpha:1] CGColor];
UIColor* outerColor = [[UIColor colorWithRed:0.08 green:0.42 blue:0.32 alpha:1] CGColor];
[self.layer setCornerRadius: 10.0];
[self.layer setMasksToBounds:YES];
NSArray* colors = [[NSArray alloc] initWithObjects: radialColor, outerColor, nil];
CGFloat endRadius = sqrt(pow(self.frame.size.width / 1.9, 2) + pow(self.frame.size.height / 1.9, 2));
CGPoint startCenter = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 12);
CGPoint center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
CGGradientRef gradient = CGGradientCreateWithColors(nil, (CFArrayRef)colors, nil);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawRadialGradient(context, gradient, startCenter, 0.0, center, endRadius, 0);
}
@end
Swift
@IBDesignable class RadialGradientView: UIView {
@IBInspectable var radialColor: UIColor = UIColor(red: 0.93, green: 0.95, blue: 0.22, alpha: 1.0)
@IBInspectable var outerColor: UIColor = UIColor(red: 0.08, green: 0.42, blue: 0.32, alpha: 1.0)
override func draw(_ rect: CGRect) {
layer.cornerRadius = 10.0
layer.masksToBounds = true
let colors = [radialColor.cgColor, outerColor.cgColor]
let endRadius = sqrt(pow(frame.width/1.9, 2) + pow(frame.height/1.9, 2))
let startCenter = CGPoint(x: bounds.size.width / 2, y: bounds.size.height / 12)
let center = CGPoint(x: bounds.size.width / 2, y: bounds.size.height / 2)
let gradient = CGGradient(colorsSpace: nil, colors: colors as CFArray, locations: nil)
let context = UIGraphicsGetCurrentContext()
context?.drawRadialGradient(gradient!, startCenter: startCenter, startRadius: 0.0, endCenter: center, endRadius: endRadius, options: CGGradientDrawingOptions.drawsBeforeStartLocation)
}
}
Screenshot
Try the code below. Make RadialGradientView
as IBDesignable
.
RadialGradientView.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
IB_DESIGNABLE
@interface RadialGradientView : UIView
@end
NS_ASSUME_NONNULL_END
RadialGradientView.m
#import "RadialGradientView.h"
#import <CoreGraphics/CoreGraphics.h>
@implementation RadialGradientView
- (void)drawRect:(CGRect)rect {
// Drawing code
UIColor* radialColor = [[UIColor colorWithRed:0.93 green:0.95 blue:0.22 alpha:1] CGColor];
UIColor* outerColor = [[UIColor colorWithRed:0.08 green:0.42 blue:0.32 alpha:1] CGColor];
[self.layer setCornerRadius: 10.0];
[self.layer setMasksToBounds:YES];
NSArray* colors = [[NSArray alloc] initWithObjects: radialColor, outerColor, nil];
CGFloat endRadius = sqrt(pow(self.frame.size.width / 1.9, 2) + pow(self.frame.size.height / 1.9, 2));
CGPoint startCenter = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 12);
CGPoint center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
CGGradientRef gradient = CGGradientCreateWithColors(nil, (CFArrayRef)colors, nil);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawRadialGradient(context, gradient, startCenter, 0.0, center, endRadius, 0);
}
@end
Swift
@IBDesignable class RadialGradientView: UIView {
@IBInspectable var radialColor: UIColor = UIColor(red: 0.93, green: 0.95, blue: 0.22, alpha: 1.0)
@IBInspectable var outerColor: UIColor = UIColor(red: 0.08, green: 0.42, blue: 0.32, alpha: 1.0)
override func draw(_ rect: CGRect) {
layer.cornerRadius = 10.0
layer.masksToBounds = true
let colors = [radialColor.cgColor, outerColor.cgColor]
let endRadius = sqrt(pow(frame.width/1.9, 2) + pow(frame.height/1.9, 2))
let startCenter = CGPoint(x: bounds.size.width / 2, y: bounds.size.height / 12)
let center = CGPoint(x: bounds.size.width / 2, y: bounds.size.height / 2)
let gradient = CGGradient(colorsSpace: nil, colors: colors as CFArray, locations: nil)
let context = UIGraphicsGetCurrentContext()
context?.drawRadialGradient(gradient!, startCenter: startCenter, startRadius: 0.0, endCenter: center, endRadius: endRadius, options: CGGradientDrawingOptions.drawsBeforeStartLocation)
}
}
Screenshot
answered Nov 22 '18 at 13:33
SateeshSateesh
2,1171819
2,1171819
thanks worked nice)
– Jack
Nov 22 '18 at 13:53
Glad to help you Jack 😀
– Sateesh
Nov 22 '18 at 13:57
add a comment |
thanks worked nice)
– Jack
Nov 22 '18 at 13:53
Glad to help you Jack 😀
– Sateesh
Nov 22 '18 at 13:57
thanks worked nice)
– Jack
Nov 22 '18 at 13:53
thanks worked nice)
– Jack
Nov 22 '18 at 13:53
Glad to help you Jack 😀
– Sateesh
Nov 22 '18 at 13:57
Glad to help you Jack 😀
– Sateesh
Nov 22 '18 at 13:57
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425773%2fhow-to-make-radial-gradient-to-be-faded-in-the-middle-of-view-in-objective-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Cannot validate it (hence a comment and not an answer) but I guess you have to set
gradient.locations = @[@0, @0.5, @1.0];
. You can remove start and end points (the default values are left-middle, right-middle which is what you need). As a sidenote, the image posted seems to be a radial gradient, with the center of the yellowish circle at middle-top.– Alladinian
Nov 22 '18 at 12:00
i did as you said but gradient.locations = @[@0, @0.5, @1.0]; makes vertical gradient
– Jack
Nov 22 '18 at 13:23