anchorPoint如何影响UIView的中心?

时间:2015-03-06 19:53:15

标签: ios iphone cocoa-touch uiview

我正在开发一款应用,可让用户使用UIGestureRecognizers调整照片大小和旋转照片。我有这个代码根据用户应用触摸的位置调整anchorPoint(使它看起来像是在他们的手指实际位置缩放图像):

- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
    UIView *gestureRecognizerView = gestureRecognizer.view;
    CGPoint locationInView = [gestureRecognizer locationInView:gestureRecognizerView];
    CGPoint locationInSuperview = [gestureRecognizer locationInView:gestureRecognizerView.superview];

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

稍后,我只是想根据中心计算原点并使用此代码进行界定:

CGRect transformedBounds = CGRectApplyAffineTransform(view.bounds, view.transform);
CGPoint origin = CGPointMake(view.center.x - (transformedBounds.size.width * view.layer.anchorPoint.x), view.center.y - (transformedBounds.size.height * view.layer.anchorPoint.y));

并且它出现错误(我正在与具有讽刺意味的是无效的帧值进行比较,但实际上确实具有正确的值)。

总而言之,我想知道,我在这里没有考虑到什么? anchorPoint如何以一种我无法确定的方式影响中心?

1 个答案:

答案 0 :(得分:0)

我认为问题在于你计算的origin并不是真正的起源,而是你transformedBounds rect起源的偏移量。

我还没有对它进行过全面的测试,但是如果你做了这样的事情,你应该得到正确的框架:

  CGRect transformedBounds =
      CGRectApplyAffineTransform(view.bounds, view.transform);
  CGSize originOffset = CGSizeMake(
      view.center.x - (transformedBounds.size.width * view.layer.anchorPoint.x),
      view.center.y -
          (transformedBounds.size.height * view.layer.anchorPoint.y));

  transformedBounds.origin.x += originOffset.width;
  transformedBounds.origin.y += originOffset.height;