Skip to content

Commit

Permalink
Merge pull request #24 from NickEntin/entin/child-animation-immutable…
Browse files Browse the repository at this point in the history
…-subelement

Enable adding child animations for immutable subelements
  • Loading branch information
NickEntin authored Mar 26, 2020
2 parents 3a60fcc + 4f31145 commit 5807b6d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
7 changes: 2 additions & 5 deletions Example/Stagehand/ChildAnimationsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,9 @@ extension ChildAnimationsViewController {

// MARK: - Public Properties

// Due to a limitation in how Swift KeyPaths are appended, these properties need to be mutable since they will
// be used for a child animation.
let leftView: UIView = .init()

var leftView: UIView = .init()

var rightView: UIView = .init()
let rightView: UIView = .init()

// MARK: - UIView

Expand Down
15 changes: 10 additions & 5 deletions Sources/Stagehand/Animation/Animation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public struct Animation<ElementType: AnyObject> {
/// in the range (0,(1 - relativeStartTimestamp)], where 0 is the beginning of the animation and 1 is the end.
public mutating func addChild<SubelementType: AnyObject>(
_ childAnimation: Animation<SubelementType>,
for subelement: WritableKeyPath<ElementType, SubelementType>,
for subelement: KeyPath<ElementType, SubelementType>,
startingAt relativeStartTimestamp: Double,
relativeDuration: Double
) {
Expand Down Expand Up @@ -646,10 +646,15 @@ extension Animation {
}
}

func mapForParent<ParentElementType>(
_ subelementPath: WritableKeyPath<ParentElementType, ElementType>
func mapForParent<ParentElementType: AnyObject>(
_ subelementPath: KeyPath<ParentElementType, ElementType>
) -> (PartialKeyPath<ParentElementType>, Animation<ParentElementType>.KeyframeSeries<PropertyType>) {
let mappedProperty = subelementPath.appending(path: property)
// This is not a type-safe cast because `appending(path:)` doesn't know that the `ParentElementType` is a
// reference type. Given the `AnyObject` restriction, it should be safe to assume that we will always get
// back a `ReferenceWritableKeyPath` since we're appending a writable key path to a key path for a reference
// type.
let mappedProperty = subelementPath.appending(path: property) as! ReferenceWritableKeyPath<ParentElementType, PropertyType>

return (mappedProperty, .init(
property: mappedProperty,
valuesByRelativeTimestamp: valuesByRelativeTimestamp
Expand All @@ -675,7 +680,7 @@ extension Animation {
_ subelementPath: PartialKeyPath<ParentElementType>
) -> (PartialKeyPath<ParentElementType>, AnyKeyframeSeries) {
let (keyPath, keyframeSeries) = mapForParent(
subelementPath as! WritableKeyPath<ParentElementType, ElementType>
subelementPath as! KeyPath<ParentElementType, ElementType>
)
return (keyPath, keyframeSeries)
}
Expand Down

0 comments on commit 5807b6d

Please sign in to comment.