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

时间:2015-10-28 08:15:32

标签: ios objective-c animation cgaffinetransformscale

我已经为背景图像做了一个Scaling动画,其中有一个箭头,通过缩放图像(它包括箭头)来表示它的整个背景。它从背景中心缩放。但我想从箭头的中心开始缩放。

我想实现这个目标:

enter image description here

然后成为规模:

enter image description here

我需要在代码中更改内容:

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

    } completion:^(BOOL finished) {

    }];

使用我当前的代码,比例从背景中心开始,所以箭头在右边(屏幕边界外)结束。

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

- (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更改为看起来像箭头中心的内容即可。