UIImageView Anchor Point Visualization

Is there an easy way to place a label (like a cross) on an anchor point of a UIImageView? I am trying to line up several rotating images of their anchor point, and being able to see these points will make the job a lot easier.

Thank you very much.

+4
source share
1 answer

You ask how to visualize the anchor point in the view, but it seems to me that you are asking for this so that you can coordinate the anchor points. I will try to answer both questions.

Anchor point visualization.

Each iOS view has a basic level that has a reference point. The anchor point is in the unit coordinate space of the layer (x and y go from 0 to 1). This means that you can multiply d by width and y by height to get the position of the anchor point inside the layer in the coordinate space of the view / layer. You can place a subspecies / sublevel there to show the location of the anchor point.

In code, you can do something similar to display a small black dot where the anchor point is.

CALayer *anchorPointLayer = [CALayer layer]; anchorPointLayer.backgroundColor = [UIColor blackColor].CGColor; anchorPointLayer.bounds = CGRectMake(0, 0, 6, 6); anchorPointLayer.cornerRadius = 3; CGPoint anchor = viewWithVisibleAnchorPoint.layer.anchorPoint; CGSize size = viewWithVisibleAnchorPoint.layer.bounds.size; anchorPointLayer.position = CGPointMake(anchor.x * size.width, anchor.y * size.height); [viewWithVisibleAnchorPoint.layer addSublayer:anchorPointLayer]; 

You can see the result in the image below for four different revolutions.

View rotated around anchor point

Aligning layers by their snap point

It's cool, but it's actually easier than aligning anchor points.

The main trick is that position and anchorPoint always the same point, only in two different coordinate spaces. The position is set in the coordinate space of the superlayer. An anchor point is indicated in the unit coordinate space of the layer.

The nice thing is that views that have alignment of position properties automatically align anchorPoint . Please note that the content is referenced relative to the anchor point. The following is an example of many representations that have the same y-component in their position , so they are aligned with y.

There really is no special code for this. Just make sure the position properties are aligned.

Multiple views aligned by their anchor point.

+6
source

Source: https://habr.com/ru/post/1491593/


All Articles