【问题标题】:How does anchorPoint influence center in a UIView?锚点如何影响 UIView 中的中心?
【发布时间】:2015-03-06 19:53:14
【问题描述】:

我正在开发一个应用程序,该应用程序允许用户使用 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 如何以我无法确定的方式影响中心?

【问题讨论】:

  • 点赞this
  • @DavidRönnqvist - 谢谢你的帮助

标签: ios iphone cocoa-touch uiview


【解决方案1】:

我认为问题在于您正在计算的 origin 并不是真正的原点,而是您的 transformedBounds 矩形原点的偏移量。

我还没有完全测试它,但如果你这样做,你应该得到正确的框架:

  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;

【讨论】:

    猜你喜欢
    • 2012-12-10
    • 1970-01-01
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    相关资源
    最近更新 更多