UnitPoint
A normalized 2D point in a view's coordinate space.
Declaration
type UnitPoint =
| 'bottom'
| 'bottomLeading'
| 'bottomTrailing'
| 'center'
| 'leading'
| 'top'
| 'topLeading'
| 'topTrailing'
| 'trailing'
| 'zero'
| { x: number; y: number; }
| { x?: number; y?: number; };@frozen struct UnitPointPass a literal, or build one with SwiftUI.UnitPoint from import { SwiftUI } from 'swiftui-react-native'.
Type properties
'bottom'SwiftUI.UnitPoint.bottomA point that's centered horizontally on the bottom edge of a view.
'bottomLeading'SwiftUI.UnitPoint.bottomLeadingA point that's in the bottom, leading corner of a view.
'bottomTrailing'SwiftUI.UnitPoint.bottomTrailingA point that's in the bottom, trailing corner of a view.
'center'SwiftUI.UnitPoint.centerA point that's centered in a view.
'leading'SwiftUI.UnitPoint.leadingA point that's centered vertically on the leading edge of a view.
'top'SwiftUI.UnitPoint.topA point that's centered horizontally on the top edge of a view.
'topLeading'SwiftUI.UnitPoint.topLeadingA point that's in the top, leading corner of a view.
'topTrailing'SwiftUI.UnitPoint.topTrailingA point that's in the top, trailing corner of a view.
'trailing'SwiftUI.UnitPoint.trailingA point that's centered vertically on the trailing edge of a view.
'zero'SwiftUI.UnitPoint.zeroThe origin of a view.
Overview
Use a unit point to represent a location in a view without having to know
the view's rendered size. The point stores a value in each dimension that
indicates the fraction of the view's size in that dimension --- measured
from the view's origin --- where the point appears. For example, you can
create a unit point that represents the center of any view by using the
value 0.5 for each dimension:
let unitPoint = UnitPoint(x: 0.5, y: 0.5)To project the unit point into the rendered view's coordinate space, multiply each component of the unit point with the corresponding component of the view's size:
let projectedPoint = CGPoint(
x: unitPoint.x * size.width,
y: unitPoint.y * size.height
)You can perform this calculation yourself if you happen to know a view's size, or if you want to use the unit point for some custom purpose, but SwiftUI typically does this for you to carry out operations that you request, like when you:
- Transform a shape using a shape modifier. For example, to rotate a
shape with
Shape.rotation(_:anchor:), you indicate an anchor point that you want to rotate the shape around. - Override the alignment of the view in a
Gridcell using theView.gridCellAnchor(_:)view modifier. The grid aligns the projection of a unit point onto the view with the projection of the same unit point onto the cell. - Create a gradient that has a center, or start and stop points, relative
to the shape that you are styling. See the gradient methods in
ShapeStyle.
You can create custom unit points with explicit values, like the example
above, or you can use one of the built-in unit points that SwiftUI provides,
like zero, center, or topTrailing. The built-in values
correspond to the alignment positions of the similarly named, built-in
Alignment types.
projects to a point outside of the view.
Layout direction
When a person configures their device to use a left-to-right language like
English, the system places the view's origin in its top-left corner,
with positive x toward the right and positive y toward the bottom of the
view. In a right-to-left environment, the origin moves to the upper-right
corner, and the positive x direction changes to be toward the left. You
don't typically need to do anything to handle this change, because SwiftUI
applies the change to all aspects of the system. For example, see the
discussion about layout direction in HorizontalAlignment.
It’s important to test your app for the different locales that you distribute your app in. For more information about the localization process, see doc://com.apple.documentation/documentation/Xcode/localization.
From Apple’s SwiftUI documentation. Code samples there are Swift.
Used by
- AngularGradientAn angular gradient.
- EllipticalGradientA radial gradient that draws an ellipse.
- LinearGradientA linear gradient.
- RadialGradientA radial gradient.
- RotatedShapeA shape with a rotation transform applied to it.
- ScaledShapeA shape with a scale transform applied to it.
- accessibilityActivationPointThe activation point for an element is the location assistive technologies use to initiate gestures.
- accessibilityDragPointThe point an assistive technology should use to begin a drag interaction.
- accessibilityDropPointThe point an assistive technology should use to end a drag interaction.
- defaultScrollAnchorAssociates an anchor to control which part of the scroll view's content should be rendered by default.
- gridCellAnchorSpecifies a custom alignment anchor for a view that acts as a grid cell.
- matchedGeometryEffectDefines a group of views with synchronized geometry using an identifier and namespace that you provide.
- rotation3DEffectRenders a view's content as if it's rotated in three dimensions around the specified axis.
- rotationEffectRotates a view's rendered output in two dimensions around the specified point.
- scaleEffectScales this view's rendered output by the given amount in both the horizontal and vertical directions, relative to an anchor point.
- scrollPositionAssociates a binding to a scroll position with a scroll view within this view.
- rotationRotates this shape around an anchor point at the angle you specify.
- scaleScales this shape without changing its bounding frame.
- sizeReturns a new version of self representing the same shape, but that will ask it to create its path from a rect of size. This does not affect the layout properties of any views created from the shape (e.g. by filling it).
- transactionApplies the given transaction mutation function to all animations used within the view.
See also
More in Drawing and shapes.
- AngleA geometric angle whose value you access in either radians or degrees.
- AngularGradientAn angular gradient.
- AnyGradientA color gradient.
- BlendModeModes for compositing a view with overlapping content.
- ButtonBorderShapeA shape used to draw a button's border.
- ColorMatrixA matrix to use in an RGBA color transformation.
- ConcentricRectangleAnimatableDataThe type defining the data to animate.
- ContainerRelativeShapeA shape that is replaced by an inset version of the current container shape. If no container shape was defined, is replaced by a rectangle.
- ContentShapeKindsA kind for the content shape of a view.
- CoordinateSpaceProtocolA frame of reference within the layout system.
- DefaultGlassEffectShapeThe default shape applied by glass effects, a capsule.
- EllipticalGradientA radial gradient that draws an ellipse.