diff --git a/README.md b/README.md index ccf0797..78f2ba1 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ Run `carthage update` to build the framework and drag the built `Framezilla.fram - [x] Edges with superview - [x] Width / Height - [x] Top / Left / Bottom / Right -- [x] CenterX / CenterY / Center (between views) +- [x] CenterX / CenterY / Center between views / Center on arc - [x] SizeToFit / SizeThatFits / WidthToFit / HeightToFit - [x] Container - [x] Stack @@ -181,6 +181,18 @@ view.configureFrame { maker in } ``` +### Center on arc + +![](img/centeredArc.png) + +If you need to do something like this, you can do: +```swift +view.configureFrame { maker in + maker.centerX(to: view1, radius: 0.5 * view1.bounds.width, angle: -.pi / 4.0) + maker.size(width: 30, height: 30) +} +``` + ## SizeToFit and SizeThatFits Very often you should configure labels, so there are some methods for comfortable work with them. diff --git a/Sources/Maker.swift b/Sources/Maker.swift index 8800cca..63a7798 100644 --- a/Sources/Maker.swift +++ b/Sources/Maker.swift @@ -795,6 +795,23 @@ public final class Maker { .centerY(to: RelationView(view: view, relation: .centerY)) } + /// Creates center relation rotated around center of a specified view. + /// + /// Use this method when you want to center view by both axis relativity another view. + /// + /// - parameter view: The view on which you set center relation. + /// - parameter radius: Radius of the arc on which center point would be placed. + /// - parameter angle: Angle at which center point will be placed. + /// + /// - returns: `Maker` instance for chaining relations. + + @discardableResult public func center(to view: UIView, radius: CGFloat, angle: CGFloat) -> Maker { + let offsetX = -radius * cos(-angle) + let offsetY = radius * sin(-angle) + return centerX(to: RelationView(view: view, relation: .centerX), offset: offsetX) + .centerY(to: RelationView(view: view, relation: .centerY), offset: offsetY) + } + /// Creates centerY relation. /// /// Use this method when you want to join centerY of current view with centerY of superview. diff --git a/Tests/MakerCenterTests.swift b/Tests/MakerCenterTests.swift index 453669d..40a795b 100644 --- a/Tests/MakerCenterTests.swift +++ b/Tests/MakerCenterTests.swift @@ -337,4 +337,26 @@ class MakerCenterTests: BaseTest { } XCTAssertEqual(testingView.frame, CGRect(x: 225, y: 225, width: 50, height: 50)) } + + func testThatCorrectlyConfiguresCenterToAnotherViewWithArc() { + testingView.configureFrame { maker in + maker.center(to: nestedView2, radius: 20.0, angle: 0.0) + } + XCTAssertEqual(testingView.frame, CGRect(x: 245, y: 225, width: 50, height: 50)) + + testingView.configureFrame { maker in + maker.center(to: nestedView2, radius: 20.0, angle: 0.5 * .pi) + } + XCTAssertEqual(testingView.frame, CGRect(x: 225, y: 245, width: 50, height: 50)) + + testingView.configureFrame { maker in + maker.center(to: nestedView2, radius: 20.0, angle: .pi) + } + XCTAssertEqual(testingView.frame, CGRect(x: 205, y: 225, width: 50, height: 50)) + + testingView.configureFrame { maker in + maker.center(to: nestedView2, radius: 20.0, angle: 1.5 * .pi) + } + XCTAssertEqual(testingView.frame, CGRect(x: 225, y: 205, width: 50, height: 50)) + } } diff --git a/img/centeredArc.png b/img/centeredArc.png new file mode 100644 index 0000000..2dac786 Binary files /dev/null and b/img/centeredArc.png differ