CALayer中的锚点 [英] Anchor Point in CALayer

查看:158
本文介绍了CALayer中的锚点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看Apple文档中的Touches示例,有以下方法:

Looking at the Touches example from Apple's documentation, there is this method:

// scale and rotation transforms are applied relative to the layer's anchor point
// this method moves a gesture recognizer's view's anchor point between the user's fingers
- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        UIView *piece = gestureRecognizer.view;
        CGPoint locationInView = [gestureRecognizer locationInView:piece];
        CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview];

        piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
        piece.center = locationInSuperview;
    }
}

第一个问题,有人可以解释设置的逻辑子视图中的锚点,并更改超视图的中心(就像为什么这样做)?

First question, can someone explain the logic of setting the anchor point in the subview, and changing the center of the superview (like why this is done)?

最后,数学如何为anchorPoint语句工作?如果您的视图的边界为500,500,并且您说触摸100,100用一根手指触摸,500,500用另一根触摸。在此框中,您的常规锚点为(250,250)。现在是???? (不知道)

Lastly, how does the math work for the anchorPoint statement? If you have a view that has a bounds of 500, 500, and say you touch at 100, 100 with one finger, 500, 500 with the other. In this box your normal anchor point is (250, 250). Now it's ???? (have no clue)

谢谢!

推荐答案

center 视图的属性仅仅反映了其后备层的位置属性。令人惊讶的是,这意味着中心不必位于视图的中心。其中 position 位于其边界内的是基于 anchorPoint ,它接收(0,0)和之间的任何值。 (1,1)。可以将其视为位置是否在其范围内的标准化指标。如果您要更改 anchorPoint 位置,则边界将自行调整,而不是将位置转换为其超级层 / superview 。因此,要重新调整位置,以便视图的框架不会移动,可以操纵中心

The center property of a view is a mere reflection of the position property of its backing layer. Surprisingly what this means is that the center need not be at the center of your view. Where position is situated within its bounds is based on the anchorPoint which takes in values anywhere between (0,0) and (1,1). Think of it as a normalized indicator of whether the position lies within its bounds. If you were to change either the anchorPoint or the position, the bounds will adjust itself rather than the position shifting w.r.t to its superlayer/superview. So to readjust position so that the frame of the view doesn't shift one can manipulate the center.

piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);

想象一下原来的东西是 O 是触摸点,

Imagine the original thing being where O is the touch point,

+++++++++++  
+ O       +         +++++++++++
+    X    +  -->    + X       +
+         +         +         +
+++++++++++         +         +
                    +++++++++++

现在我们希望这个 X 处于用户触摸的位置。我们这样做是因为所有缩放和旋转都是基于位置 / anchorPoint 完成的。要将帧调整回原始位置,我们将视图的center设置为触摸位置。

Now we want this X to be at the point where the user has touched. We do this because all scaling and rotations are done based on the position/anchorPoint. To adjust the frame back to its original position, we set the "center" of the view to the touch location.

piece.center = locationInSuperview;

所以这反映在视图中重新调整其框架,

So this reflects in the view readjusting its frame back,

                    +++++++++++  
+++++++++++         + X       +
+ X       +  -->    +         +
+         +         +         +
+         +         +++++++++++
+++++++++++

现在,当用户旋转或缩放时,就会发生轴,就好像轴位于触摸点而不是视图的真实中心。

Now when the user rotates or scales, it will happen as if the axis were at the touch point rather than the true center of the view.

在您的示例中,视图的位置可能最终为平均值,即(300,300),这意味着 anchorPoint 将是(0.6,0.6),作为回应,框架将向上移动。要重新调整,我们将中心移动到触摸位置会将框架移回。

In your example, the location of view might end up being the average i.e. (300, 300) which means the anchorPoint would be (0.6, 0.6) and in response the frame will move up. To readjust we move the center to the touch location will move the frame back down.

这篇关于CALayer中的锚点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆