I am creating a ring with multiple color segments. Here's my code:
CGFloat viewWidth = rect.size.width;
CGFloat viewHeight = rect.size.height;
CGFloat lineWidth = viewWidth * 0.15f;
CGFloat radius = viewWidth/2 - lineWidth;
CGPoint center = CGPointMake(viewWidth/2, viewHeight/2);
UIColor *lavender = [UIColor colorWithRed:0.5f green:0.0f blue:1.0f alpha:1.0f];
UIColor *purple = [UIColor purpleColor];
UIColor *fuchsia = [UIColor colorWithRed:1.0 green:0.0f blue:0.5f alpha:1.0f];
UIColor *red = [UIColor redColor];
UIColor *orange = [UIColor orangeColor];
UIColor *yellow = [UIColor yellowColor];
UIColor *yellowGreen = [UIColor colorWithRed:0.5f green:1.0f blue:0.0f alpha:1.0f];
UIColor *green = [UIColor greenColor];
UIColor *blueGreen = [UIColor colorWithRed:0.0f green:1.0f blue:0.5f alpha:1.0f];
UIColor *cyan = [UIColor cyanColor];
UIColor *white = [UIColor whiteColor];
UIColor *lightBlue = [UIColor colorWithRed:0.0f green:0.5f blue:1.0f alpha:1.0f];
UIColor *blue = [UIColor blueColor];
NSArray *colors = #[lavender, purple, fuchsia,
red, orange, yellow,
yellowGreen, green, blueGreen,
cyan, white, lightBlue, blue];
for(int i = 0; i < 13; i++)
{
CGFloat startAngle = 0.1538f * M_PI * i;
CGFloat endAngle = 0.1538f * M_PI * (i + 1);
UIColor *strokeColor = [colors objectAtIndex:i];
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
[bezierPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
CAShapeLayer *colorPieceLayer = [[CAShapeLayer alloc] init];
[colorPieceLayer setPath:bezierPath.CGPath];
[colorPieceLayer setStrokeColor:strokeColor.CGColor];
[colorPieceLayer setFillColor:[UIColor clearColor].CGColor];
[colorPieceLayer setLineWidth:lineWidth];
[self.layer addSublayer:colorPieceLayer];
}
I tried:
setting the background color of the view self.backgroundColor = [UIColor clearColor];
setting the background color of the layer self.layer.backgroundColor = [UIColor clearColor].CGColor
setting the background color of the sublayers colorPieceLayer.backgroundColor = [UIColor clearColor].CGColor
setting the opacity (But this will make the view disappear).
Here's my screenshot:
Here's my view layers:
So how am I going to make it clear color????
PLEASE TAKE NOTE:
The purple colored is just a UIView that holds my color ring view.
So when I change my view's background color, this color changed.
You could try it.
Related
I have an arc that goes around in a circle and changes colors three times. I used an example on SO to get me started: Change CALayer color while animating
This example works, however the colors are red, then green then blue. I would like them to be Green, Orange, then Red. I'm trying to figure out how in the code he changed colors, and it's quite confusing to me:
for (NSUInteger i = 0; i < 3; i++)
{
CAShapeLayer* strokePart = [[CAShapeLayer alloc] init];
strokePart.fillColor = [[UIColor clearColor] CGColor];
strokePart.frame = tutorialCircle.bounds;
strokePart.path = tutorialCircle.path;
strokePart.lineCap = tutorialCircle.lineCap;
strokePart.lineWidth = tutorialCircle.lineWidth;
// These could come from an array or whatever, this is just easy...
strokePart.strokeColor = [[UIColor colorWithHue: i * portion saturation:1 brightness:1 alpha:1] CGColor];
So I believe this line is doing the color change:
strokePart.strokeColor = [[UIColor colorWithHue: i * portion saturation:1 brightness:1 alpha:1] CGColor];
My goal is to manipulate those colors to Green Orange Red instead of Red Green and Blue.
Thanks
EDIT:
This is the value of portion:
const double portion = 1.0 / ((double)3);
The HUE-Color system defines the color in a an angle of 360 degrees, where 0 is red, 108 (360*0,3) is a green and is a blue. This is how the colors get generated:
strokePart.strokeColor = [[UIColor colorWithHue: 0 saturation:1 brightness:1 alpha:1] CGColor]; // RED
strokePart.strokeColor = [[UIColor colorWithHue: 0.3 saturation:1 brightness:1 alpha:1] CGColor]; // GREEN
strokePart.strokeColor = [[UIColor colorWithHue: 0.6 saturation:1 brightness:1 alpha:1] CGColor]; // BLUE
If you want to change to colors, you could shift the portion by some other value:
strokePart.strokeColor = [[UIColor colorWithHue: 0.3 + portion saturation:1 brightness:1 alpha:1] CGColor];
Or you could define yourself an array, with your colors you want to go through:
NSArray* colors = #[ UIColor.green, UIColor.orange, UIColor.red ];
for (id color in colors)
{
CAShapeLayer* strokePart = [[CAShapeLayer alloc] init];
strokePart.fillColor = [[UIColor clearColor] CGColor];
strokePart.frame = tutorialCircle.bounds;
strokePart.path = tutorialCircle.path;
strokePart.lineCap = tutorialCircle.lineCap;
strokePart.lineWidth = tutorialCircle.lineWidth;
// These could come from an array or whatever, this is just easy...
strokePart.strokeColor = [color CGColor];
Ah I just figured it out. Here is the answer below:
if (i == 0) {
strokePart.strokeColor = [UIColor greenColor].CGColor;
}
else if (i == 1) {
strokePart.strokeColor = [UIColor orangeColor].CGColor;
}
else if (i == 2) {
strokePart.strokeColor = [UIColor redColor].CGColor;
}
Using below code for shadow in outside for UIButton. But how can i get shadow for inside the button
button.imageView.layer.cornerRadius = 7.0f;
button.layer.shadowRadius = 3.0f;
button.layer.shadowColor = [UIColor blackColor].CGColor;
button.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
button.layer.shadowOpacity = 0.5f;
button.layer.masksToBounds = NO;
#define kDEFAULT_SHADOW_COLOR [UIColor lightGrayColor]
UIColor *color = [kDEFAULT_SHADOW_COLOR colorWithAlphaComponent:0.3f];
NSArray *colorsArray = #[(id)[color CGColor], (id)[[UIColor clearColor] CGColor]];
CGFloat yOffset = 0.0f;
CGFloat leftHeight = button.bounds.size.height;
CAGradientLayer *shadow;
shadow = [CAGradientLayer layer];
shadow.colors = colorsArray;
shadow.frame = CGRectMake(0, yOffset, 5.0, leftHeight);
shadow.startPoint = CGPointMake(0.0, 0.5);
shadow.endPoint = CGPointMake(1.0, 0.5);
[button.layer insertSublayer:shadow atIndex:0];
You have not implemented the button shadows property. Add the following lines of code and show shadow. The Code is...
self.submitBtn.layer.masksToBounds = YES;
self.submitBtn.layer.clipsToBounds = YES;
self.submitBtn.backgroundColor = [UIColor colorWithRed:(200.0f/255.0f) green:0.0 blue:0.0 alpha:1.0];
self.submitBtn.layer.cornerRadius = 3.0;
self.submitBtn.layer.borderWidth = 2.0;
self.submitBtn.layer.borderColor = [[UIColor clearColor] CGColor];
self.submitBtn.layer.shadowColor = [UIColor colorWithRed:(100.0f/255.0f) green:0.0 blue:0.0 alpha:1.0].CGColor;
self.submitBtn.layer.shadowOpacity = 1.0f;
self.submitBtn.layer.shadowRadius = 1.0f;
self.submitBtn.layer.shadowOffset = CGSizeMake(0, 3);
Right now I have this code, and I want to be able to change the color gradually as the angle of an arc changes.
I want to gradually change from green, to yellow, then orange, then red. But gradually, not just at once.
Any ideas on how to do this?
UIColor *greenColor = [UIColor greenColor];
greenColor = [UIColor colorWithRed:.0 green:1 blue:.0 alpha:0.5];
UIColor *grayColor = [UIColor grayColor];
grayColor = [UIColor colorWithRed:.5 green:.5 blue:.5 alpha:0.5];
UIColor *redColor = [UIColor redColor];
redColor = [UIColor colorWithRed:1.0 green:.0 blue:.0 alpha:0.5];
UIColor *yellowColor = [UIColor yellowColor];
yellowColor = [UIColor colorWithRed:1.0 green:1.0 blue:.0 alpha:0.5];
UIColor *orangeColor = [UIColor orangeColor];
orangeColor = [UIColor colorWithRed:1.0 green:0.647 blue:.0 alpha:0.5];
int direction = 0;
if(index == 3){
direction = 1;
CGFloat currentEndAngle = atan2f(d_y, d_x);
CGFloat actualEndAngle = -0.49234065359; //was M_PI
if(fabs(currentEndAngle*180/M_PI) <= 91){
// Draw the optimal arc
//[self drawArc:ourPoint.startPosition andEndAngle:actualEndAngle andRadius:radius andColor:grayColor andDirection:direction withContext:context];
if(currentEndAngle > 0 || currentEndAngle > actualEndAngle){
// Draw the current green arc
if(currentEndAngle - actualEndAngle < 0.6){
[self drawArc:ourPoint.startPosition andEndAngle:currentEndAngle andRadius:radius andColor:orangeColor andDirection:direction withContext:context];
}
else{
[self drawArc:ourPoint.startPosition andEndAngle:currentEndAngle andRadius:radius andColor:greenColor andDirection:direction withContext:context];
}
}
else{
// Draw the current red arc
[self drawArc:ourPoint.startPosition andEndAngle:currentEndAngle andRadius:radius andColor:redColor andDirection:direction withContext:context];
missAngle += fabs(currentEndAngle-actualEndAngle);
}
}
As Ckouta says in his answer, what you want is a gradient. I don't think his code would do exactly what you want to do (drawing an arc with a color gradient) but it's a step in the right direction.
Getting an arc to draw using a gradient would be tricky. You'd either need to create custom Core Graphics code to render this yourself, use some combination of CALayers, layer masks, and CAGradientLayers, or maybe use Core Image filters to apply a radial gradient to an arc image.
I just want to draw circle with stroke, the code below draws well but it fills the circle.
I do not want it filled. Please help me
self.circleView = [[UIView alloc] initWithFrame:CGRectMake(10,20,20,20)];
circleView.alpha = 0.5;
self.circleView.layer.cornerRadius = 50;
self.circleView.backgroundColor = [UIColor whiteColor];
change
self.circleView.backgroundColor = [UIColor whiteColor];
to
self.circleView.backgroundColor = [UIColor clearColor];
and for the border (Stroke) add
self.circleView.layer.borderColor = [[UIColor redColor] CGColor];
self.circleView.layer.borderWidth = 1;
Following some instructions at my first post, what I am doing is
1. create a class ( subclass of uiview)
2. fill it with gradient
3. add a uilabel on it
What I am doing is :
1. in xib, i drag and drop uiview onto the screen
2. change to 'customViewBackGround' in 'custom class'
and customViewbackGround looks like below :
-(void) layoutSubviews {
CGFloat height = 20.0;
CGFloat x = 5;
CGFloat y = 3;
CGRect rect = CGRectMake(x, y, self.bounds.size.width - 2 * x, height);
titleLabel.frame = rect;
titleLabel = [[UILabel alloc] init] ;
titleLabel.textAlignment = NSTextAlignmentLeft;
titleLabel.opaque = NO;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.font = [UIFont systemFontOfSize:15];
titleLabel.textColor = [UIColor blackColor];
[self addSubview:titleLabel];
}
- (void)drawRect:(CGRect)rect {
titleLabel.text = #"How to redeem your e-gift card online";
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef whiteColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0].CGColor;
CGColorRef lightGrayColor = [UIColor colorWithRed:230.0/255.0
green:230.0/255.0
blue:230.0/255.0
alpha:1.0].CGColor;
CGColorRef separatorColor = [UIColor colorWithRed:208.0/255.0 green:208.0/255.0 blue:208.0/255.0 alpha:1.0].CGColor;
CGRect paperRect = self.bounds;
// Fill with gradient
drawLinearGradient(context, paperRect, whiteColor, lightGrayColor);
// Add white 1 px stroke
CGRect strokeRect = paperRect;
strokeRect.size.height -= 1;
strokeRect = rectFor1PxStroke(strokeRect);
CGContextSetStrokeColorWithColor(context, whiteColor);
CGContextSetLineWidth(context, 1.0);
CGContextStrokeRect(context, strokeRect);
// Add separator
CGPoint startPoint = CGPointMake(paperRect.origin.x, paperRect.origin.y + paperRect.size.height - 1);
CGPoint endPoint = CGPointMake(paperRect.origin.x + paperRect.size.width - 1, paperRect.origin.y + paperRect.size.height - 1);
draw1PxStroke(context, startPoint, endPoint, separatorColor);
}
However, the text of uilabel is not showing up after all.
Any ideas about this issue.
You can't set the frame of titleLabel before it's been initialized. Swap those two lines:
titleLabel = [[UILabel alloc] init] ;
titleLabel.frame = rect;
In layoutSubviews you are setting the frame on titleLabel before it is created. Set it after creating the label.