How to set corner radius value with CAShaperLayer? - ios

i drew a vertical line with UIBeizerPath and i need this line to have a corner radius with a value of 5. i tried to recall [pathLayer setCornerRadius: 5]; but I do not get results ... Can you help me? how can I assign a corner radius value? this is the code I use
// crea le barre del grafico e gli assegna l'altezza della label y corrispondente
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(xPaddingFromYAxis +spaceBetweenBar *j, 200)];
[path addLineToPoint:CGPointMake(xPaddingFromYAxis +spaceBetweenBar *j, yLabelValue.center.y )];
CAShapeLayer *pathLayer = [CAShapeLayer layer];
pathLayer.frame = self.bounds;
pathLayer.path = path.CGPath;
pathLayer.strokeColor = [UIColor darkGrayColor].CGColor;
pathLayer.fillColor = nil;
pathLayer.lineWidth = 50;
[pathLayer setCornerRadius:5];
pathLayer.masksToBounds = NO;
[scroll.layer addSublayer:pathLayer];

You need to set mask to bound as 'YES'. pathLayer.masksToBounds = YES;
Try this and see:
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(xPaddingFromYAxis +spaceBetweenBar *j, 200)];
[path addLineToPoint:CGPointMake(xPaddingFromYAxis +spaceBetweenBar *j, yLabelValue.center.y )];
CAShapeLayer *pathLayer = [CAShapeLayer layer];
pathLayer.frame = self.bounds;
pathLayer.path = path.CGPath;
pathLayer.strokeColor = [UIColor darkGrayColor].CGColor;
pathLayer.fillColor = nil;
pathLayer.lineWidth = 50;
[pathLayer setCornerRadius:5];
pathLayer.masksToBounds = YES;
[scroll.layer addSublayer:pathLayer];

Related

How to append arrow head to UIBezierPath

I need help in adding arrow head to UIBezierPath.
I have created multiple lines by using UIBezierPath, which are in different directions.
Now, I want to create arrow head at the end point of the line.
I tried to add UIImage to UILable using NSAttributedString to UILable with NSTextAttachment.
But, here problem is arrow head is always down. And I want to align arrow along the line.
Here is my code:
UIBezierPath *ShotPath = [UIBezierPath bezierPath];
NSValue *PointValue = [[[PathArray objectAtIndex:i] valueForKey:#"ShotPath"] objectAtIndex:k];
CGPoint FirstPoint; = [PointValue CGPointValue];
[ShotPath moveToPoint:FirstPoint];
CAShapeLayer *line = [CAShapeLayer layer];
line.lineWidth = 4.0;
line.path=ShotPath.CGPath;
line.fillColor = [UIColor clearColor].CGColor;
line.strokeColor = [UIColor colorWithRed:231/255.0 green:83/255.0 blue:73/255.0 alpha:1].CGColor;
[[BaseView layer] addSublayer:line];
NSValue *PointValue = [[[PathArray objectAtIndex:i] valueForKey:#"ShotPath"] objectAtIndex:[[[PathArray objectAtIndex:i] valueForKey:#"ShotPath"] count]-1];
CGPoint OtherPoint = [PointValue CGPointValue];
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(OtherPoint.x-6, OtherPoint.y-8, 12, 12)];
lbl.backgroundColor =[UIColor clearColor];
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:#"DropDown_Icon"];
NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
lbl.attributedText = attachmentString;
[lbl setTextColor:[UIColor blackColor]];
lbl.font =[UIFont boldSystemFontOfSize:22.0];
[BaseView addSubview:lbl];
Here is my current output :
By the reference of this answer:
append arrowhead to UIBezierPath
Solved my query:
Answer:
- (void)viewDidLoad {
[super viewDidLoad];
UIBezierPath *path = [UIBezierPath bezierPath];
CGPoint point1 = CGPointMake(100, 100);
CGPoint point2 = CGPointMake(300, 300);
[path moveToPoint:point1];
[path addQuadCurveToPoint:point2 controlPoint:point1];
CAShapeLayer *shape = [CAShapeLayer layer];
shape.path = path.CGPath;
shape.lineWidth = 10;
shape.strokeColor = [UIColor blueColor].CGColor;
shape.fillColor = [UIColor clearColor].CGColor;
shape.frame = self.view.bounds;
[self.view.layer addSublayer:shape];
CGFloat angle = atan2f(point2.y - point1.y, point2.x - point1.x);
CGFloat distance = 15.0;
path = [UIBezierPath bezierPath];
[path moveToPoint:point2];
[path addLineToPoint:[self calculatePointFromPoint:point2 angle:angle + M_PI_2 distance:distance]]; // to the right
[path addLineToPoint:[self calculatePointFromPoint:point2 angle:angle distance:distance]]; // straight ahead
[path addLineToPoint:[self calculatePointFromPoint:point2 angle:angle - M_PI_2 distance:distance]]; // to the left
[path closePath];
shape = [CAShapeLayer layer];
shape.path = path.CGPath;
shape.lineWidth = 2;
shape.strokeColor = [UIColor redColor].CGColor;
shape.fillColor = [UIColor redColor].CGColor;
shape.frame = self.view.bounds;
[self.view.layer addSublayer:shape];
}
- (CGPoint)calculatePointFromPoint:(CGPoint)point angle:(CGFloat)angle distance:(CGFloat)distance {
return CGPointMake(point.x + cosf(angle) * distance, point.y + sinf(angle) * distance);
}

Setting the lineWidth of a UIBezierPath not working

I'm trying to draw a circle but the circle border always the same thin no matter what value I set.
It feel like the line with always the default thinnest width.
- (void)drawCircleAtPoint:(CGPoint)center radius: (CGFloat)radius{
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 4; // Here
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineJoinRound;
CGFloat r = radius;
[path addArcWithCenter:center radius:r startAngle:0.0 endAngle:M_PI*2 clockwise:true];
[path stroke];
CAShapeLayer* layer = [CAShapeLayer new];
layer.path = path.CGPath;
layer.strokeColor = UIColor.redColor.CGColor;
layer.fillColor = UIColor.yellowColor.CGColor;
[layer addAnimation:[self getAnimation] forKey:nil];
[self.layer addSublayer:layer];
}
- (CABasicAnimation*)getAnimation {
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:#"strokeEnd"];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.duration = 1.2;
animation.fromValue = #0;
animation.toValue = #1;
return animation;
}
This really drove me crazy.
You need to change the width of CAShapeLayer and not UIBezierPath. Like this :
- (void)drawCircleAtPoint:(CGPoint)center radius: (CGFloat)radius{
CGFloat r = radius;
UIBezierPath *path = [UIBezierPath bezierPath];
[path addArcWithCenter:center radius:r startAngle:0.0 endAngle:M_PI*2 clockwise:true];
[path setLineWidth:4]; // No need
[path setLineCapStyle:kCGLineCapRound];
[path setLineJoinStyle:kCGLineJoinRound];
[path stroke];
CAShapeLayer *layer = [CAShapeLayer new];
layer.lineWidth = 4; // Add it here
layer.path = path.CGPath;
layer.strokeColor = UIColor.redColor.CGColor;
layer.fillColor = UIColor.yellowColor.CGColor;
[layer addAnimation:[self getAnimation] forKey:nil];
[self.view.layer addSublayer:layer];
}

UIBezierPath - Rectangle with rounded corners

I'm using dr-Charts API to draw bar graphs in my application. I'm drawing the bar graph:
BarChart *barChartView = [[BarChart alloc] initWithFrame:CGRectMake(0, 150, WIDTH(self.view), HEIGHT(self.view) - 600)];
[barChartView setDataSource:self];
[barChartView setDelegate:self];
[barChartView setLegendViewType:LegendTypeHorizontal];
[barChartView setShowCustomMarkerView:TRUE];
[barChartView drawBarGraph];
[barChartView setDrawGridY:TRUE];
[barChartView setDrawGridX:FALSE];
[self.view addSubview:barChartView];
Actually, I want my bar chart rectangles to be rounded rect, something similar to this:
So, In BarChart.m, I started playing with API CAShapeLayer & UIBezierPath:
- (UIBezierPath *)drawBarPathWithStartPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint{
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(startPoint.x, startPoint.y, endPoint.x - startPoint.x, endPoint.y - startPoint.y)];
//UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(startPoint.x, startPoint.y, endPoint.x - startPoint.x, endPoint.y - startPoint.y) byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(10, 10)];
[path stroke];
return path;
}
CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
[shapeLayer setPath:[[self drawBarPathWithStartPoint:endPoint endPoint:startPoint] CGPath]];
[shapeLayer setStrokeColor:[barData.barColor CGColor]];
[shapeLayer setFillColor:[barData.barColor CGColor]];
[shapeLayer setFillRule:kCAFillRuleEvenOdd];
[shapeLayer setLineWidth:0.5f];
[shapeLayer setOpacity:0.7f];
[shapeLayer setShadowRadius:0.0f];
[shapeLayer setShadowColor:[[UIColor clearColor] CGColor]];
[shapeLayer setShadowOpacity:0.0f];
[shapeLayer setValue:[barData.yAxisArray objectAtIndex:i] forKey:#"data"];
After following many answers on StackOverflow, I couldn't succeed.
I tried many answers like shapeLayer.cornerRadius = 10;
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(startPoint.x, startPoint.y, endPoint.x - startPoint.x, endPoint.y - startPoint.y) byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(10, 10)];
Could you please help.
Try to draw wide line with rounded corners, for example
UIBezierPath *linePath = [UIBezierPath bezierPath];
[linePath moveToPoint:CGPointMake(topX, topY)];
[linePath addLineToPoint:CGPointMake(bottomX, bottomY)];
CAShapeLayer *line = [CAShapeLayer layer];
line.frame = some_frame;
line.lineCap = kCALineCapRound;
line.path = linePath.CGPath;
line.fillColor = nil;
line.lineWidth = 6.0;
line.cornerRadius = 3.0;
line.opacity = 1.0;
line.strokeColor = [UIColor greenColor].CGColor;
[line setMasksToBounds:YES];

How to draw trapezoid shape in iOS?

I need help to draw trapezoid shapes in iOS.
I am using the following code to draw the Pink shape, but it is not working:
UIBezierPath* trianglePath = [UIBezierPath bezierPath];
[trianglePath moveToPoint:CGPointMake(0, 0)];
[trianglePath addLineToPoint:CGPointMake(topView.frame.size.width, topView.frame.size.height/2)];
[trianglePath addLineToPoint:CGPointMake(100, topView.frame.size.height/2)];
CAShapeLayer *triangleMaskLayer = [CAShapeLayer layer];
[triangleMaskLayer setPath:trianglePath.CGPath];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,topView.frame.size.height-150, 200, 150)];
view.layer.mask = triangleMaskLayer;
[self.view addSubview:view];
How can I draw the pink-colored shape?
-(void)TriangularPinkView{
UIBezierPath *path = [UIBezierPath bezierPath];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.strokeColor = [[UIColor redColor] CGColor];
shapeLayer.lineWidth = 0.5;
CGPoint start = CGPointMake(CGRectGetMinX(self.pinkView.frame), CGRectGetMidY(self.pinkView.frame)+CGRectGetHeight(self.pinkView.frame)*.25);
[path moveToPoint:start];
[path addLineToPoint:CGPointMake(CGRectGetMaxX(self.pinkView.frame), CGRectGetMidY(self.pinkView.frame))];
[path addLineToPoint:CGPointMake(CGRectGetMaxX(self.pinkView.frame), CGRectGetMaxY(self.pinkView.frame))];
[path addLineToPoint:CGPointMake(CGRectGetMinX(self.pinkView.frame), CGRectGetMaxY(self.pinkView.frame))];
shapeLayer.path = [path CGPath];
// shapeLayer.fillRule = kCAFillRuleEvenOdd;
shapeLayer.fillColor = [[UIColor redColor] CGColor];
[self.view.layer addSublayer:shapeLayer];
}

[iOS]Layer on UILabel, why is not full?

I custom a BWLabel to draw the label's border.
- (void)drawRect:(CGRect)rect {
CGRect frame = self.frame;
UIBezierPath *linePath = [UIBezierPath bezierPath];
[linePath moveToPoint:CGPointMake(0,0)];
[linePath addLineToPoint:CGPointMake(frame.size.width, 0)];
[linePath addLineToPoint:CGPointMake(frame.size.width, frame.size.height)];
[linePath addLineToPoint:CGPointMake(0, frame.size.height)];
CAShapeLayer *lineLayer = [CAShapeLayer layer];
lineLayer.lineWidth = 3.0f;
lineLayer.strokeColor = [[UIColor whiteColor] CGColor];
lineLayer.path = linePath.CGPath;
[self.layer addSublayer:lineLayer];
}
I use Debug View Hierarchy to see the BWLabel.
I have a question, why the CAShapeLayer is not full?
your draw rect should look like this
- (void)drawRect:(CGRect)rect
{
CGRect frame = self.frame;
UIBezierPath *linePath = [UIBezierPath bezierPath];
[linePath moveToPoint:CGPointMake(0,0)];
[linePath addLineToPoint:CGPointMake(frame.size.width, 0)];
[linePath addLineToPoint:CGPointMake(frame.size.width, frame.size.height)];
[linePath addLineToPoint:CGPointMake(0, frame.size.height)];
[linePath addLineToPoint:CGPointMake(0, 0)];
CAShapeLayer *lineLayer = [CAShapeLayer layer];
lineLayer.lineWidth = 3.0f;
lineLayer.strokeColor = [[UIColor whiteColor] CGColor];
lineLayer.path = linePath.CGPath;
[self.layer addSublayer:lineLayer];
}

Resources