Skip to content

Commit

Permalink
Support corner radius in rectangles
Browse files Browse the repository at this point in the history
  • Loading branch information
calda committed Nov 2, 2022
1 parent 6af679e commit c6c1be3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
16 changes: 13 additions & 3 deletions Sources/Private/CoreAnimation/Animations/RectangleAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ extension CAShapeLayer {
func addAnimations(
for rectangle: Rectangle,
context: LayerAnimationContext,
pathMultiplier: PathMultiplier)
pathMultiplier: PathMultiplier,
roundedCorners: RoundedCorners?)
throws
{
let combinedKeyframes = try rectangle.combinedKeyframes(
context: context,
roundedCorners: roundedCorners)

try addAnimation(
for: .path,
keyframes: try rectangle.combinedKeyframes(context: context).keyframes,
keyframes: combinedKeyframes.keyframes,
value: { keyframe in
BezierPath.rectangle(
position: keyframe.position.pointValue,
Expand All @@ -37,7 +42,12 @@ extension Rectangle {
}

/// Creates a single array of animatable keyframes from the separate arrays of keyframes in this Rectangle
func combinedKeyframes(context: LayerAnimationContext) throws-> KeyframeGroup<Rectangle.Keyframe> {
func combinedKeyframes(
context: LayerAnimationContext,
roundedCorners: RoundedCorners?) throws
-> KeyframeGroup<Rectangle.Keyframe>
{
let cornerRadius = roundedCorners?.radius ?? cornerRadius
let combinedKeyframes = Keyframes.combinedIfPossible(
size, position, cornerRadius,
makeCombinedResult: Rectangle.Keyframe.init)
Expand Down
18 changes: 11 additions & 7 deletions Sources/Private/CoreAnimation/Animations/ShapeAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ extension CAShapeLayer {
pathMultiplier: PathMultiplier,
roundedCorners: RoundedCorners?) throws
{
if roundedCorners != nil {
try context.compatibilityAssert(shape is Shape, """
Rounded corners support is currently only implemented for Shape items
""")
}

switch shape {
case let customShape as Shape:
try addAnimations(
Expand All @@ -28,15 +22,25 @@ extension CAShapeLayer {

case let combinedShape as CombinedShapeItem:
try addAnimations(for: combinedShape, context: context, pathMultiplier: pathMultiplier)
try context.compatibilityAssert(roundedCorners == nil, """
Rounded corners support is not currently implemented for combined shape items
""")

case let ellipse as Ellipse:
try addAnimations(for: ellipse, context: context, pathMultiplier: pathMultiplier)

case let rectangle as Rectangle:
try addAnimations(for: rectangle, context: context, pathMultiplier: pathMultiplier)
try addAnimations(
for: rectangle,
context: context,
pathMultiplier: pathMultiplier,
roundedCorners: roundedCorners)

case let star as Star:
try addAnimations(for: star, context: context, pathMultiplier: pathMultiplier)
try context.compatibilityAssert(roundedCorners == nil, """
Rounded corners support is currently not implemented for polygon items
""")

default:
// None of the other `ShapeItem` subclasses draw a `path`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ extension KeyframeGroup {
/// - In those sorts of cases, we currently choose one one `KeyframeGroup` to provide the
/// timing information, and disallow simultaneous animations on the other properties.
///
/// - We could support animating all of the values simultaneously if we manually
/// interpolated the property for each individual frame, like we do in
/// `CombinedShapeItem.manuallyInterpolating` and `BezierPathKeyframe.combining`
///
func exactlyOneKeyframe(
context: CompatibilityTrackerProviding,
description: String,
Expand Down

0 comments on commit c6c1be3

Please sign in to comment.