Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added LayerTransformAnimation #55

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ This will produce an effect where the view will be fully faded in and visible fo
* **LayerStrokeEndAnimation** animates the `strokeEnd` property of a `CAShapeLayer` _(does not work with LayerStrokeStartAnimation)_.
* **LayerFillColorAnimation** animates the `fillColor` property of a `CAShapeLayer`.
* **LayerStrokeColorAnimation** animates the `strokeColor` property of a `CAShapeLayer`.
* **LayerTransformAnimation** animates the `transform` property of a `CALayer`.
* **PathPositionAnimation** animates the `layer.position` property of a `UIView` along a path.
* **LabelTextColorAnimation** animates the `textColor` property of a `UILabel`.
* **ConstraintConstantAnimation** animates an `AutoLayout` constraint constant.
Expand Down
36 changes: 36 additions & 0 deletions Source/Interpolatable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,39 @@ extension Bool : Interpolatable {
return toValue
}
}

extension CATransform3D : Interpolatable {

/**
Find a CATransform3D at a certain progress point (from 0 to 1) between two other CATransform3Ds.

- parameter fromValue: The starting CATransform3D
- parameter toValue: The ending CATransform3D
- parameter progress: The progress (from 0 to 1) from the starting CATransform3D to the end CATransform3D for this CATransform3D

- returns: The CATransform3D at the given progress point between the given starting and ending CATransform3D
*/

public static func interpolateFrom(_ fromValue: CATransform3D, to toValue: CATransform3D, withProgress progress: CGFloat) -> CATransform3D {
assert((0 <= progress) && (progress <= 1), "Progress must be between 0 and 1")

return CATransform3D(
m11: CGFloat.interpolateFrom(fromValue.m11, to: toValue.m11, withProgress: progress),
m12: CGFloat.interpolateFrom(fromValue.m12, to: toValue.m12, withProgress: progress),
m13: CGFloat.interpolateFrom(fromValue.m13, to: toValue.m13, withProgress: progress),
m14: CGFloat.interpolateFrom(fromValue.m14, to: toValue.m14, withProgress: progress),
m21: CGFloat.interpolateFrom(fromValue.m21, to: toValue.m21, withProgress: progress),
m22: CGFloat.interpolateFrom(fromValue.m22, to: toValue.m22, withProgress: progress),
m23: CGFloat.interpolateFrom(fromValue.m23, to: toValue.m23, withProgress: progress),
m24: CGFloat.interpolateFrom(fromValue.m24, to: toValue.m24, withProgress: progress),
m31: CGFloat.interpolateFrom(fromValue.m31, to: toValue.m31, withProgress: progress),
m32: CGFloat.interpolateFrom(fromValue.m32, to: toValue.m32, withProgress: progress),
m33: CGFloat.interpolateFrom(fromValue.m33, to: toValue.m33, withProgress: progress),
m34: CGFloat.interpolateFrom(fromValue.m34, to: toValue.m34, withProgress: progress),
m41: CGFloat.interpolateFrom(fromValue.m41, to: toValue.m41, withProgress: progress),
m42: CGFloat.interpolateFrom(fromValue.m42, to: toValue.m42, withProgress: progress),
m43: CGFloat.interpolateFrom(fromValue.m43, to: toValue.m43, withProgress: progress),
m44: CGFloat.interpolateFrom(fromValue.m44, to: toValue.m44, withProgress: progress)
)
}
}
19 changes: 19 additions & 0 deletions Source/LayerTransformAnimation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

import UIKit

/**
Animates the `transform` property of a `CALayer`.
*/
public class LayerTransformAnimation : Animation<CATransform3D>, Animatable {
private let layer : CALayer

public init(layer: CALayer) {
self.layer = layer
}

public func animate(_ time: CGFloat) {
if !hasKeyframes() {return}

layer.transform = self[time]
}
}