pub struct CAShapeLayerBuilder { /* private fields */ }Expand description
Builder for CAShapeLayer.
§Basic Usage
let shape = CAShapeLayerBuilder::new()
.path(circle_path)
.fill_color(Color::RED)
.stroke_color(Color::WHITE)
.line_width(2.0)
.build();§With Animations
Animations can be added inline using the .animate() method:
let shape = CAShapeLayerBuilder::new()
.path(circle_path)
.fill_color(Color::RED)
.animate("pulse", KeyPath::TransformScale, |a| {
a.values(0.85, 1.15)
.duration(800.millis())
.easing(Easing::InOut)
.autoreverses()
.repeat(Repeat::Forever)
})
.build();Multiple animations can be added:
CAShapeLayerBuilder::new()
.fill_color(Color::RED)
.animate("pulse", KeyPath::TransformScale, |a| {
a.values(0.9, 1.1).duration(500.millis()).repeat(Repeat::Forever)
})
.animate("fade", KeyPath::Opacity, |a| {
a.values(1.0, 0.7).duration(1.seconds()).repeat(Repeat::Forever)
})
.build()Implementations§
Source§impl CAShapeLayerBuilder
impl CAShapeLayerBuilder
Sourcepub fn path(self, path: CFRetained<CGPath>) -> Self
pub fn path(self, path: CFRetained<CGPath>) -> Self
Sets the path to render.
Sourcepub fn circle(self, diameter: CGFloat) -> Self
pub fn circle(self, diameter: CGFloat) -> Self
Creates a perfect circle path and sets appropriate bounds.
This is a convenience method that wraps CGPath::with_ellipse_in_rect
and automatically sets the layer’s bounds to match the circle size.
§Arguments
diameter- The diameter of the circle
§Examples
// Create a circle with 80pt diameter
let circle = CAShapeLayerBuilder::new()
.circle(80.0)
.position(CGPoint::new(100.0, 100.0))
.fill_color(Color::CYAN)
.build();§Notes
This method sets both the path and bounds. If you call .bounds() or
.path() after .circle(), those values will override what was set
by .circle().
§See Also
Use ellipse for non-circular ellipses.
Sourcepub fn ellipse(self, width: CGFloat, height: CGFloat) -> Self
pub fn ellipse(self, width: CGFloat, height: CGFloat) -> Self
Creates an ellipse path and sets appropriate bounds.
This is a convenience method that wraps CGPath::with_ellipse_in_rect
and automatically sets the layer’s bounds to match the ellipse size.
§Arguments
width- The width of the ellipse bounding boxheight- The height of the ellipse bounding box
§Examples
// Create an ellipse (oval shape)
let ellipse = CAShapeLayerBuilder::new()
.ellipse(100.0, 60.0) // wider than tall
.position(CGPoint::new(100.0, 100.0))
.fill_color(Color::RED)
.build();§Notes
This method sets both the path and bounds. If you call .bounds() or
.path() after .ellipse(), those values will override what was set
by .ellipse().
§See Also
Use circle for perfect circles (same width and height).
Sourcepub fn fill_color(self, color: impl Into<CFRetained<CGColor>>) -> Self
pub fn fill_color(self, color: impl Into<CFRetained<CGColor>>) -> Self
Sets the fill color.
Accepts any type that implements Into<CFRetained<CGColor>>, including:
Color::RED,Color::rgb(1.0, 0.0, 0.0)CFRetained<CGColor>directly
Sourcepub fn fill_rgba(self, r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat) -> Self
pub fn fill_rgba(self, r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat) -> Self
Sets the fill color from RGBA values (0.0–1.0).
Sourcepub fn stroke_color(self, color: impl Into<CFRetained<CGColor>>) -> Self
pub fn stroke_color(self, color: impl Into<CFRetained<CGColor>>) -> Self
Sets the stroke color.
Accepts any type that implements Into<CFRetained<CGColor>>, including:
Color::WHITE,Color::rgb(1.0, 1.0, 1.0)CFRetained<CGColor>directly
Sourcepub fn stroke_rgba(self, r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat) -> Self
pub fn stroke_rgba(self, r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat) -> Self
Sets the stroke color from RGBA values (0.0–1.0).
Sourcepub fn line_width(self, width: CGFloat) -> Self
pub fn line_width(self, width: CGFloat) -> Self
Sets the stroke line width.
Sourcepub fn transform(self, transform: CATransform3D) -> Self
pub fn transform(self, transform: CATransform3D) -> Self
Sets the 3D transform.
Sets whether the layer is hidden.
Sourcepub fn shadow_color(self, color: impl Into<CFRetained<CGColor>>) -> Self
pub fn shadow_color(self, color: impl Into<CFRetained<CGColor>>) -> Self
Sourcepub fn shadow_offset(self, dx: f64, dy: f64) -> Self
pub fn shadow_offset(self, dx: f64, dy: f64) -> Self
Sourcepub fn shadow_radius(self, radius: f64) -> Self
pub fn shadow_radius(self, radius: f64) -> Self
Sourcepub fn shadow_opacity(self, opacity: f32) -> Self
pub fn shadow_opacity(self, opacity: f32) -> Self
Sourcepub fn scale(self, scale: f64) -> Self
pub fn scale(self, scale: f64) -> Self
Sets a uniform scale transform.
This is applied using CATransform3D internally.
§Examples
// Scale to 80% of original size
CAShapeLayerBuilder::new()
.scale(0.8)
.build();§Notes
When multiple transform shortcuts are set, they are composed in order: scale → rotation → translation.
If you also call .transform(), the explicit transform takes
precedence and scale/rotation/translate are ignored.
Sourcepub fn rotation(self, radians: f64) -> Self
pub fn rotation(self, radians: f64) -> Self
Sets a z-axis rotation transform (in radians).
This is applied using CATransform3D internally.
For degrees, use: .rotation(45.0_f64.to_radians())
§Examples
use std::f64::consts::PI;
// Rotate 45 degrees
CAShapeLayerBuilder::new()
.rotation(PI / 4.0)
.build();
// Or using to_radians()
CAShapeLayerBuilder::new()
.rotation(45.0_f64.to_radians())
.build();§Notes
When multiple transform shortcuts are set, they are composed in order: scale → rotation → translation.
If you also call .transform(), the explicit transform takes
precedence and scale/rotation/translate are ignored.
Sourcepub fn translate(self, dx: f64, dy: f64) -> Self
pub fn translate(self, dx: f64, dy: f64) -> Self
Sets a translation transform (dx, dy).
This is applied using CATransform3D internally.
§Examples
// Move 10 points right and 20 points up
CAShapeLayerBuilder::new()
.translate(10.0, 20.0)
.build();§Notes
When multiple transform shortcuts are set, they are composed in order: scale → rotation → translation.
If you also call .transform(), the explicit transform takes
precedence and scale/rotation/translate are ignored.
Sourcepub fn animate<F>(
self,
name: impl Into<String>,
key_path: KeyPath,
configure: F,
) -> Self
pub fn animate<F>( self, name: impl Into<String>, key_path: KeyPath, configure: F, ) -> Self
Adds an animation to be applied when the layer is built.
The animation is configured using a closure that receives a
CABasicAnimationBuilder and returns the configured builder.
§Arguments
name- A unique identifier for this animation (used as the animation key)key_path- The property to animate (e.g.,KeyPath::TransformScale)configure- A closure that configures the animation builder
§Examples
// Simple pulse animation
CAShapeLayerBuilder::new()
.path(circle_path)
.fill_color(Color::RED)
.animate("pulse", KeyPath::TransformScale, |a| {
a.values(0.85, 1.15)
.duration(800.millis())
.easing(Easing::InOut)
.autoreverses()
.repeat(Repeat::Forever)
})
.build();
// Multiple animations on the same layer
CAShapeLayerBuilder::new()
.fill_color(Color::BLUE)
.animate("scale", KeyPath::TransformScale, |a| {
a.values(0.9, 1.1).duration(500.millis()).repeat(Repeat::Forever)
})
.animate("fade", KeyPath::Opacity, |a| {
a.values(1.0, 0.5).duration(1.seconds()).repeat(Repeat::Forever)
})
.build();Sourcepub fn build(self) -> Retained<CAShapeLayer> ⓘ
pub fn build(self) -> Retained<CAShapeLayer> ⓘ
Builds and returns the configured CAShapeLayer.
All pending animations added via .animate() are applied to the layer.