从另一个锚点在背景中缩放动画

Scale animation in background from another anchorPoint

我已经为背景图像制作了一个带有箭头的缩放动画,通过缩放图像(它包括箭头)意味着它是整个背景。它从背景的中心开始缩放。但我想从箭头的中心开始缩放。

我想实现这个:

然后变成带比例的:

我必须在代码中更改的内容:

    self.blueBackground.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
    [UIView animateWithDuration:5 animations:^{
        self.blueBackground.transform = CGAffineTransformScale(CGAffineTransformIdentity, 30, 30);

    } completion:^(BOOL finished) {

    }];

使用我当前的代码,比例尺从背景中心开始,因此箭头在右侧结束(超出屏幕范围)。

尝试这样的事情:

- (void)animateArrow
{
  self.blueBackground.transform = CGAffineTransformIdentity;
  [UIView animateWithDuration:5 animations:^{
    ScaleViewAboutPointInBounds(self.blueBackground, arrowCenter, 30);
  } completion:^(BOOL finished) {
  }];
}

static void ScaleViewAboutPointInBounds(UIView *view, CGPoint point, CGFloat scaleAmount)
{
  CGFloat xAnchor = view.layer.anchorPoint.x * CGRectGetWidth(view.bounds);
  CGFloat yAnchor = view.layer.anchorPoint.y * CGRectGetHeight(view.bounds);

  CGFloat xOffset = point.x - xAnchor;
  CGFloat yOffset = point.y - yAnchor;

  CGAffineTransform shift = CGAffineTransformMakeTranslation(-xOffset, -yOffset);
  CGAffineTransform unshift = CGAffineTransformMakeTranslation(xOffset, yOffset);
  CGAffineTransform scale = CGAffineTransformMakeScale(scaleAmount, scaleAmount);

  view.transform = CGAffineTransformConcat(shift, CGAffineTransformConcat(scale, unshift));
}

它可能比您需要的更通用,您可以做得更简单,但这也应该有效。只需将 point 更改为看起来像箭头中心的东西即可。