Skip to content

Commit 54c0451

Browse files
authored
Merge pull request #114 from mirego/fix_frame_center_and_layer_anchorPoint
Handle correctly view's `layer.anchorPoint`
2 parents e0930ca + 28af2c3 commit 54c0451

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

Sources/Impl/Coordinates.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ class Coordinates {
9494
the view's transform. So view's transforms won't be affected/altered by PinLayout.
9595
*/
9696
let adjustedRect = Coordinates.adjustRectToDisplayScale(rect)
97-
view.center = CGPoint(x: adjustedRect.midX, y: adjustedRect.midY)
97+
98+
// NOTE: The center is offset by the layer.anchorPoint, so we have to take it into account.
99+
view.center = CGPoint(x: adjustedRect.origin.x + (adjustedRect.width * view.layer.anchorPoint.x),
100+
y: adjustedRect.origin.y + (adjustedRect.height * view.layer.anchorPoint.y))
98101
view.bounds = CGRect(origin: .zero, size: adjustedRect.size)
99102
}
100103

Tests/TransformSpec.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,5 +455,45 @@ class TransformSpec: QuickSpec {
455455
expect(rootView.bounds).to(equal(CGRect(x: 0, y: 0, width: 400.0, height: 400.0)))
456456
}
457457
}
458+
459+
describe("Modifying layer.anchorPoint change the view.center position") {
460+
it("aView: Default layer.anchorPoint (0.5, 0.5)") {
461+
aView.pin.top(100).left(100).width(100).height(100)
462+
463+
expect(aView.frame).to(equal(CGRect(x: 100, y: 100, width: 100.0, height: 100.0)))
464+
expect(aView.bounds).to(equal(CGRect(x: 0, y: 0, width: 100.0, height: 100.0)))
465+
expect(aView.center).to(equal(CGPoint(x: 150, y: 150)))
466+
}
467+
468+
it("aView: With layer.anchorPoint change (0.25, 0.25)") {
469+
aView.layer.anchorPoint = CGPoint(x: 0.25, y: 0.25) // default is 0.5, 0.5 (Center)
470+
aView.pin.top(100).left(100).width(100).height(100)
471+
472+
expect(aView.frame).to(equal(CGRect(x: 100, y: 100, width: 100.0, height: 100.0)))
473+
expect(aView.bounds).to(equal(CGRect(x: 0, y: 0, width: 100.0, height: 100.0)))
474+
expect(aView.center).to(equal(CGPoint(x: 125, y: 125)))
475+
}
476+
477+
it("aView: With layer.anchorPoint change (1, 1)") {
478+
aView.layer.anchorPoint = CGPoint(x: 1, y: 1) // default is 0.5, 0.5 (Center)
479+
aView.pin.top(100).left(100).width(100).height(100)
480+
481+
expect(aView.frame).to(equal(CGRect(x: 100, y: 100, width: 100.0, height: 100.0)))
482+
expect(aView.bounds).to(equal(CGRect(x: 0, y: 0, width: 100.0, height: 100.0)))
483+
expect(aView.center).to(equal(CGPoint(x: 200, y: 200)))
484+
}
485+
486+
it("aView: With layer.anchorPoint change (0, 0) + transform scale 2") {
487+
aView.layer.anchorPoint = CGPoint(x: 0, y: 0) // default is 0.5, 0.5 (Center)
488+
aView.transform = CGAffineTransform(scaleX: 2, y: 2)
489+
490+
aView.pin.top(100).left(100).width(100).height(100)
491+
492+
expect(aView.frame).to(equal(CGRect(x: 100, y: 100, width: 200.0, height: 200.0)))
493+
expect(aView.bounds).to(equal(CGRect(x: 0, y: 0, width: 100.0, height: 100.0)))
494+
expect(aView.center).to(equal(CGPoint(x: 100, y: 100)))
495+
}
496+
497+
}
458498
}
459499
}

0 commit comments

Comments
 (0)