Skip to content

Commit

Permalink
Add support for centering a view along an arc
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Kormakov committed Nov 27, 2019
1 parent 69278a1 commit f914917
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions Sources/Maker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,23 @@ public final class Maker {
.centerY(to: RelationView<VerticalRelation>(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<HorizontalRelation>(view: view, relation: .centerX), offset: offsetX)
.centerY(to: RelationView<VerticalRelation>(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.
Expand Down
22 changes: 22 additions & 0 deletions Tests/MakerCenterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Binary file added img/centeredArc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f914917

Please sign in to comment.