Skip to content

Commit 2244073

Browse files
committed
Add Ability to Scale or Translate X Y by CGPoint
1 parent 6bba22e commit 2244073

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

Diff for: Sources/Decomposed/CATransform3DExtensions.swift

+27
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,27 @@ public extension CATransform3D {
110110
return self.translated(by: translation)
111111
}
112112

113+
/**
114+
Returns a copy by translating the current transform by the given translation components.
115+
116+
- Note: Omitted components have no effect on the translation.
117+
*/
118+
func translated(by translation: CGPoint) -> Self {
119+
let translation = Translation(translation.x, translation.y, 0.0)
120+
return self.translated(by: translation)
121+
}
122+
113123
/// Translates the current transform by the given translation amount.
114124
mutating func translate(by translation: Translation) {
115125
self = CATransform3D(matrix.translated(by: translation.storage))
116126
}
117127

128+
/// Translates the current transform by the given translation amount.
129+
mutating func translate(by translation: CGPoint) {
130+
let translation = Translation(translation.x, translation.y, 0.0)
131+
translate(by: translation)
132+
}
133+
118134
/// The scale of the transform.
119135
var scale: Scale {
120136
get {
@@ -134,6 +150,11 @@ public extension CATransform3D {
134150
return transform
135151
}
136152

153+
/// Returns a copy by scaling the current transform by the given scale.
154+
func scaled(by scale: CGPoint) -> Self {
155+
return self.scaled(by: Scale(scale.x, scale.y, 0.0))
156+
}
157+
137158
/**
138159
Returns a copy by scaling the current transform by the given scale.
139160

@@ -149,6 +170,12 @@ public extension CATransform3D {
149170
self = CATransform3D(matrix.scaled(by: scale.storage))
150171
}
151172

173+
/// Scales the current transform by the given scale.
174+
mutating func scale(by scale: CGPoint) {
175+
let scale = Scale(scale.x, scale.y, 0.0)
176+
self.scale(by: scale)
177+
}
178+
152179
/// The rotation of the transform (expressed as a quaternion).
153180
var rotation: CGQuaternion {
154181
get {

Diff for: Tests/DecomposedTests/DoubleTests.swift

+17
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ final class DoubleTests: XCTestCase {
2323
let testTransform3 = CATransform3DIdentity.translated(by: CGVector3(x: 2.0, y: 3.0, z: 4.0))
2424

2525
XCTAssertEqual(testTransform3, testTransformCA)
26+
27+
testTransform2.translation.z = 0.0
28+
let testTransform4 = CATransform3DIdentity.translated(by: CGPoint(x: 2.0, y: 3.0))
29+
XCTAssertEqual(testTransform4, testTransform2)
30+
31+
var testTransform5 = CATransform3DIdentity
32+
testTransform5.translate(by: CGPoint(x: 2.0, y: 3.0))
33+
XCTAssertEqual(testTransform5, testTransform2)
2634
}
2735

2836
func testScale() {
@@ -41,6 +49,15 @@ final class DoubleTests: XCTestCase {
4149

4250
let testTransform3 = CATransform3DIdentity.scaled(by: CGVector3(x: 2.0, y: 3.0, z: 4.0))
4351
XCTAssertEqual(testTransform3, testTransformCA)
52+
53+
testTransform2.scale.z = 0.0
54+
55+
let testTransform4 = CATransform3DIdentity.scaled(by: CGPoint(x: 2.0, y: 3.0))
56+
XCTAssertEqual(testTransform4, testTransform2)
57+
58+
var testTransform5 = CATransform3DIdentity
59+
testTransform5.scale(by: CGPoint(x: 2.0, y: 3.0))
60+
XCTAssertEqual(testTransform5, testTransform2)
4461
}
4562

4663
func testRotation() {

Diff for: Tests/DecomposedTests/FloatTests.swift

+17
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ final class FloatTests: XCTestCase {
2323
let testTransform3 = CATransform3DIdentity.translated(by: CGVector3(x: 2.0, y: 3.0, z: 4.0))
2424

2525
XCTAssertEqual(testTransform3, testTransformCA)
26+
27+
testTransform2.translation.z = 0.0
28+
let testTransform4 = CATransform3DIdentity.translated(by: CGPoint(x: 2.0, y: 3.0))
29+
XCTAssertEqual(testTransform4, testTransform2)
30+
31+
var testTransform5 = CATransform3DIdentity
32+
testTransform5.translate(by: CGPoint(x: 2.0, y: 3.0))
33+
XCTAssertEqual(testTransform5, testTransform2)
2634
}
2735

2836
func testScale() {
@@ -41,6 +49,15 @@ final class FloatTests: XCTestCase {
4149

4250
let testTransform3 = CATransform3DIdentity.scaled(by: CGVector3(x: 2.0, y: 3.0, z: 4.0))
4351
XCTAssertEqual(testTransform3, testTransformCA)
52+
53+
testTransform2.scale.z = 0.0
54+
55+
let testTransform4 = CATransform3DIdentity.scaled(by: CGPoint(x: 2.0, y: 3.0))
56+
XCTAssertEqual(testTransform4, testTransform2)
57+
58+
var testTransform5 = CATransform3DIdentity
59+
testTransform5.scale(by: CGPoint(x: 2.0, y: 3.0))
60+
XCTAssertEqual(testTransform5, testTransform2)
4461
}
4562

4663
func testRotation() {

0 commit comments

Comments
 (0)