-
Notifications
You must be signed in to change notification settings - Fork 144
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
Add support for CALayer layout #151
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
caf6b37
Add support for CALayer layouting
antoinelamy 72654b6
Merge branch 'master' into feature/CALayer
antoinelamy c96f6cb
Add missing pin and pinframe properties on CALayer
antoinelamy 36a9fe1
Tests added
antoinelamy 95e261b
Include pinFrame, edge and anchor tests for CALayer
antoinelamy 549aba4
Change safe area tests to expect at least one safe area did change ca…
antoinelamy dc4e934
Add CALayer informations to README
antoinelamy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// | ||
// CALayer+PinLayout.swift | ||
// PinLayout | ||
// | ||
// Created by Antoine Lamy on 2018-06-20. | ||
// Copyright © 2018 mcswiftlayyout.mirego.com. All rights reserved. | ||
// | ||
|
||
import QuartzCore | ||
|
||
extension CALayer: Layoutable { | ||
public typealias View = CALayer | ||
|
||
public var superview: CALayer? { | ||
return superlayer | ||
} | ||
|
||
public var subviews: [CALayer] { | ||
return sublayers ?? [] | ||
} | ||
|
||
public var pin: PinLayout<CALayer> { | ||
return PinLayout(view: self, keepTransform: true) | ||
} | ||
|
||
public var pinFrame: PinLayout<CALayer> { | ||
return PinLayout(view: self, keepTransform: false) | ||
} | ||
|
||
public func getRect(keepTransform: Bool) -> CGRect { | ||
if keepTransform { | ||
/* | ||
To adjust the view's position and size, we don't set the UIView's frame directly, because we want to keep the | ||
view's transform (UIView.transform). | ||
By setting the view's center and bounds we really set the frame of the non-transformed view, and this keep | ||
the view's transform. So view's transforms won't be affected/altered by PinLayout. | ||
*/ | ||
|
||
let size = bounds.size | ||
// See setRect(...) for details about this calculation. | ||
let origin = CGPoint(x: position.x - (size.width * anchorPoint.x), | ||
y: position.y - (size.height * anchorPoint.y)) | ||
|
||
return CGRect(origin: origin, size: size) | ||
} else { | ||
return frame | ||
} | ||
} | ||
|
||
public func setRect(_ rect: CGRect, keepTransform: Bool) { | ||
let adjustedRect = Coordinates<View>.adjustRectToDisplayScale(rect) | ||
|
||
if keepTransform { | ||
/* | ||
To adjust the view's position and size, we don't set the UIView's frame directly, because we want to keep the | ||
view's transform (UIView.transform). | ||
By setting the view's center and bounds we really set the frame of the non-transformed view, and this keep | ||
the view's transform. So view's transforms won't be affected/altered by PinLayout. | ||
*/ | ||
|
||
// NOTE: The center is offset by the layer.anchorPoint, so we have to take it into account. | ||
position = CGPoint(x: adjustedRect.origin.x + (adjustedRect.width * anchorPoint.x), | ||
y: adjustedRect.origin.y + (adjustedRect.height * anchorPoint.y)) | ||
// NOTE: We must set only the bounds's size and keep the origin. | ||
bounds.size = adjustedRect.size | ||
} else { | ||
frame = adjustedRect | ||
} | ||
} | ||
|
||
public func isLTR() -> Bool { | ||
switch Pin.layoutDirection { | ||
case .auto: return true | ||
case .ltr: return true | ||
case .rtl: return false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// | ||
// CALayerSpec.swift | ||
// PinLayout-iOS | ||
// | ||
// Created by Antoine Lamy on 2018-06-21. | ||
// Copyright © 2018 mcswiftlayyout.mirego.com. All rights reserved. | ||
// | ||
|
||
import Quick | ||
import Nimble | ||
import PinLayout | ||
|
||
class CALayerSpec: QuickSpec { | ||
override func spec() { | ||
var viewController: PViewController! | ||
var rootView: BasicView! | ||
var rootLayer: CALayer! | ||
var aLayer: CALayer! | ||
var bLayer: CALayer! | ||
|
||
/* | ||
rootLayer | ||
| | ||
- aLayer | ||
bLayer | ||
*/ | ||
|
||
beforeSuite { | ||
_pinlayoutSetUnitTest(scale: 2) | ||
} | ||
|
||
beforeEach { | ||
Pin.lastWarningText = nil | ||
Pin.logMissingLayoutCalls = false | ||
|
||
viewController = PViewController() | ||
viewController.view = BasicView() | ||
|
||
rootView = BasicView() | ||
rootView.frame = CGRect(x: 0, y: 0, width: 400, height: 400) | ||
|
||
rootLayer = CALayer() | ||
rootLayer.frame = CGRect(x: 0, y: 0, width: 400, height: 400) | ||
|
||
aLayer = CALayer() | ||
aLayer.bounds.size = CGSize(width: 50, height: 50) | ||
bLayer = CALayer() | ||
bLayer.bounds.size = CGSize(width: 20, height: 20) | ||
|
||
#if os(macOS) | ||
rootView.wantsLayer = true | ||
rootView.layer?.addSublayer(rootLayer) | ||
#else | ||
rootView.layer.addSublayer(rootLayer) | ||
#endif | ||
|
||
rootLayer.addSublayer(aLayer) | ||
rootLayer.addSublayer(bLayer) | ||
|
||
viewController.view.addSubview(rootView) | ||
} | ||
|
||
afterEach { | ||
Pin.logMissingLayoutCalls = false | ||
} | ||
|
||
// | ||
// CALayer is already heavily tested since UIView delegate it's layout to it's layer. | ||
// Validate only the direct usability. | ||
// | ||
describe("test CALayer interface") { | ||
it("should work with basic pinlayout calls") { | ||
aLayer.pin.top(10).left(10).width(20%).height(80%) | ||
bLayer.pin.right(of: aLayer, aligned: .center) | ||
expect(aLayer.frame).to(equal(CGRect(x: 10, y: 10, width: 80, height: 320))) | ||
} | ||
|
||
it("should be able to be positioned relatively to edges") { | ||
aLayer.pin.top().right(to: rootLayer.edge.right) | ||
expect(aLayer.frame).to(equal(CGRect(x: 350, y: 0, width: 50, height: 50))) | ||
} | ||
|
||
it("should be able to be positioned relatively to anchors") { | ||
aLayer.pin.topLeft(to: rootLayer.anchor.center) | ||
expect(aLayer.frame).to(equal(CGRect(x: 200, y: 200, width: 50, height: 50))) | ||
} | ||
|
||
it("should support pinFrame properly when a transform is set") { | ||
rootLayer.transform = CATransform3DIdentity | ||
|
||
bLayer.frame = aLayer.frame | ||
|
||
aLayer.transform = CATransform3DMakeScale(2, 2, 1) | ||
bLayer.transform = CATransform3DMakeScale(2, 2, 1) | ||
|
||
aLayer.pin.top(100).left(100).width(100).height(50) | ||
bLayer.pinFrame.top(100).left(100).width(100).height(50) | ||
|
||
expect(aLayer.frame).to(equal(CGRect(x: 50, y: 75, width: 200, height: 100))) | ||
expect(aLayer.bounds).to(equal(CGRect(x: 0, y: 0, width: 100, height: 50))) | ||
expect(bLayer.frame).to(equal(CGRect(x: 100, y: 100, width: 100, height: 50))) | ||
expect(bLayer.bounds).to(equal(CGRect(x: 0, y: 0, width: 50, height: 25))) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe add unit tests that:
pinFrame
edge
propertyanchor
propertyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will also do :)