围绕不同锚点和位置的旋转使图像首先跳到新位置

时间:2015-04-08 15:11:35

标签: objective-c uiviewanimation cgaffinetransform image-rotation

我正在对视图进行旋转动画,并希望它围绕视图的中心X和底部Y旋转。我更改了anchorPoint和图层的位置并运行动画。这是我的代码:

- (void)viewDidLoad {
  [super viewDidLoad];
  _imageView = [UIImageView newAutoLayoutView];
  _imageView.image = [PCImage imageNamed:@"Umbrella"];
  [self.view addSubview:_imageView];
  [_imageView autoAlignAxisToSuperviewAxis:ALAxisVertical];
  [_imageView autoPinEdgeToSuperviewEdge:ALEdgeBottom];
}

- (void)viewDidAppear:(BOOL)animated {
  [super viewDidAppear:animated];

  CGPoint newPosition = CGPointMake(CGRectGetMidX(_imageView.frame), CGRectGetMaxY(_imageView.frame));
  NSLog(@"frame %@, new position %@", NSStringFromCGRect(_imageView.frame), NSStringFromCGPoint(newPosition));

  _imageView.layer.anchorPoint = CGPointMake(.5, 1.0);
  _imageView.layer.position = newPosition;


  [UIView animateKeyframesWithDuration:2.0 delay:2.0 options:UIViewKeyframeAnimationOptionCalculationModeLinear | UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat animations:^{
    [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:.1 animations:^{
      _imageView.transform = CGAffineTransformMakeRotation(M_PI / 64);
    }];
  } completion:nil];
}

修改

旋转正在工作,但视图首先“向上”跳到一个新位置,其中视图的底部现在是第一个布局时视图的中心Y所在的位置。我想改变anchorPoint并更新位置会阻止跳跃。视图固定在superview的下边缘,如果可能很重要,则将X放在自动布局的超视图中心X中心。有什么想法吗?

EDIT2

我已经阅读了关于此的其他好帖子,但我必须遗漏一些东西..

Scale UIView with the top center as the anchor point?

1 个答案:

答案 0 :(得分:0)

我在阅读这篇文章后最终将视图更改为不使用自动布局:

Adjust anchor point of CALayer when autolayout is used

看起来变换和自动布局并不能很好地协同工作。

_imageView = [UIImageView new];
_imageView.image = [PCImage imageNamed:@"Umbrella"];
_imageView.frame = CGRectMake(0, kScreenHeight - _imageView.image.size.height, _imageView.image.size.width, _imageView.image.size.height);
[self.view addSubview:_imageView];

在某些时候希望在那篇文章中尝试其他想法。

相关问题