Questions tagged [cashapelayer]

CAShapeLayer is a special purpose CALayer subclass for drawing animatable shapes using cubic Bezier splines. It's a part of the QuartzCore framework and is available for both iOS (since iOS 3.0) and OS X (since OS X v10.6 "Snow Leopard").

CAShapeLayer is a special purpose CALayer subclass for drawing animatable shapes using cubic Bezier splines. It's a part of the framework and is available for both (since iOS 3.0) and (since OS X v10.6 "Snow Leopard").

The shapes is defined using the path property and can be both filled and stroked using the fillColor and strokeColor properties. Note that the backgroundColor property inherited from CALayer does not fill the shape. The path of the shape is animatable (but doesn't support implicit animations) but the appearance of the animation is undefined if the two paths have a different number of control points or segments.

There are several properties (animatable) to configure how the shape is stroked and filled. Using these properties the stroke can for example get a moving dashed patten (commonly called "marching ants").

The strokeStart and strokeEnd properties can also be used to only stroke part of the shape or to animate the shape being stroked. Both values go from 0.0 (beginning of path) to 1.0 (end of path) and values in between are interpolated along the path.


The page about Paths in the Quartz 2D Programming Guide is a great resource for learning more about creating paths, stroking, rounding and joining paths, filling paths and creating dash patterns.

951 questions
80
votes
5 answers

How to draw a smooth circle with CAShapeLayer and UIBezierPath?

I am attempting to draw a stroked circle by using a CAShapeLayer and setting a circular path on it. However, this method is consistently less accurate when rendered to the screen than using borderRadius or drawing the path in a CGContextRef…
bcherry
  • 7,150
  • 2
  • 28
  • 37
43
votes
4 answers

Animate CAShapeLayer path on animated bounds change

I have a CAShapeLayer (which is the layer of a UIView subclass) whose path should update whenever the view's bounds size changes. For this, I have overridden the layer's setBounds: method to reset the path: @interface CustomShapeLayer :…
Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
36
votes
3 answers

Applying a Gradient to CAShapeLayer

Does anyone have any experience in applying a Gradient to a CAShapeLayer? CAShapeLayer is a fantastic layer class, but it appears to only support solid fill coloring, whereas I'd like it to have a gradient fill (actually an animatable gradient at…
35
votes
2 answers

An easy way to draw a circle using CAShapeLayer

In questions like How to draw a smooth circle..., ...Draw Circle... and ...draw filled Circles the question and answer is very broad, contains lots of unnecessary steps and the methods used isn't always the easiest to re-create or manage. What is an…
Aleksander Azizi
  • 9,829
  • 9
  • 59
  • 87
29
votes
7 answers

UIBezierPath Triangle with rounded edges

I have designed the following code to generate a UIBezierPath. This path is used within a CAShapeLayer to mask a UIView. Note, the view's height and width are variable. This code generates a triangle with sharp edges, but I would like to make the…
H Bellamy
  • 22,405
  • 23
  • 76
  • 114
22
votes
2 answers

Can I change a strokeEnd property without animation?

I can't seem to remove the animation from the strokeEnd property of my CAShapeLayer. The documentation says the property is animatable but not that is animated by default and I can't pinpoint the problem. Any suggestions where to look? Here's my…
Tieme
  • 62,602
  • 20
  • 102
  • 156
21
votes
1 answer

How to animate CAShapeLayer path and fillColor

How to animate CAShapeLayer path and fillColor? How to animate strokeEnd to show the path being drawn? and how to animate fillColor?
MAMN84
  • 1,400
  • 2
  • 13
  • 22
18
votes
3 answers

Mask a UIView with a cut-out circle

I’ve created a UIView with a semi-transparent black background to sit across my main app’s view. What I’m aiming for here is to create a cut-out circle shape somewhere in that UIView where the semi-transparent black background is not seen, giving…
Luke
  • 9,512
  • 15
  • 82
  • 146
18
votes
4 answers

Animating CAShapeLayer size change

I am drawing a circle, with an initial radius of 200 self.circle = [CAShapeLayer layer]; self.circle.fillColor = nil; self.circle.strokeColor = [UIColor blackColor].CGColor; self.circle.lineWidth = 7; self.circle.bounds = CGRectMake(0, 0, 2 *…
Jonathan
  • 13,947
  • 17
  • 94
  • 123
17
votes
5 answers

UIImagePickerController editing view circle overlay

I've been able to get pretty far with what I've been wanting to accomplish, and that's to replicate iOS's built in circular photo cropper for the built in contacts app. However, I'm stuck at trying to get my CAShapeLayers made correctly. I'm trying…
klcjr89
  • 5,862
  • 10
  • 58
  • 91
16
votes
1 answer

Setting correct frame of a newly created CAShapeLayer

In short: Apple does NOT set the frame or bounds for a CAShapeLayer automatically (and Apple HAS NOT implemented an equivalent of [UIView sizeThatFits]) If you set the frame using the size of the bounding-box of the path ... everything goes wrong.…
Adam
  • 32,900
  • 16
  • 126
  • 153
16
votes
5 answers

How to fill a CAShapeLayer with an angled gradient

How do I fill a CAShapeLayer() with a gradient and on an angle of 45 degrees? For example, in Image 1, the below code draws a square and fills the layer blue (UIColor.blueColor().CGColor). But, how do I fill it with a gradient on a 45 degree angle…
user4806509
  • 2,925
  • 5
  • 37
  • 72
16
votes
2 answers

How to mask and add shadow to a UIView

I'm making a custom view that i want to mask and to add shadow to it. the masking: let p = UIBezierPath() p.moveToPoint(CGPointMake(20, 20)) p.addLineToPoint(CGPointMake(100, 20)) p.addLineToPoint(CGPointMake(100, 50)) …
ilan
  • 4,402
  • 6
  • 40
  • 76
15
votes
4 answers

How to draw a circle path with color gradient stroke

I want to draw a circle with color gradient stroke like the following picture, on both iOS and macOS: Is it possible to implement with CAShapeLayer or NSBezierPath/CGPath? Or any other ways?
venj
  • 563
  • 1
  • 3
  • 14
15
votes
3 answers

Draw circle with UIBezierPath

I'm trying to draw a circle using UIBezierPath addArcWithCenter method : UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0., 0., 100., 100.)]; [bezierPath addArcWithCenter:center radius:0. …
Mosbah
  • 1,347
  • 1
  • 14
  • 28
1
2 3
63 64