【问题标题】:Return view to original position when ending UIView animation结束 UIView 动画时将视图返回到原始位置
【发布时间】:2011-12-08 02:55:04
【问题描述】:

我有一个非常简单的 UIView 动画,它会导致我的视​​图“跳动”:

[UIView animateWithDuration:0.2 delay:0.0f options:UIViewAnimationOptionAllowUserInteraction+UIViewAnimationOptionRepeat+UIViewAnimationOptionAutoreverse animations:^{
    CGAffineTransform transf = CGAffineTransformScale(self.view.transform, 1.05f, 1.05f);
    [self.view setTransform:transf];
} completion:nil];

在某些时候,用户点击按钮取消动画,并应用新的变换。

[self.view.layer removeAllAnimations];
[self.view setTransform:aNewTransform];

我希望它重置为原始变换,但它的大小却增加了 5%。

编辑:我尝试添加一个完成块,将转换重置为其原始位置。这可行,但会导致我在之后立即运行的转换被践踏......完成块在我应用aNewTransform 之后运行。

编辑 2:我找到了一个解决方案,使用 CABasicAnimation,而不是 UIView 动画。如果有人找到使用 UIView 动画的解决方案,我仍然会感兴趣......我更喜欢基于块的界面。这也只有效,因为我碰巧跟踪我的比例值与应用于视图的比例值分开。改变尺度的一切都使用了同样改变的方法self.scale

我的替换动画:

CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
basicAnimation.toValue = [NSNumber numberWithFloat:self.scale*1.05f];
basicAnimation.fromValue = [NSNumber numberWithFloat:self.scale];
basicAnimation.autoreverses = YES;
basicAnimation.duration = 0.2;
basicAnimation.repeatCount = HUGE_VALF;
[self.view.layer addAnimation:basicAnimation forKey:@"Throb"];

【问题讨论】:

    标签: ios uiview core-animation uiviewanimation


    【解决方案1】:

    在现有视图上开始新动画之前,如果之前更改了这些属性中的任何一个,您可以重置视图:

    // first, don't forget to stop ongoing animations for the view
    [theView.layer removeAllAnimations];
    
    // if the view was hidden
    theView.hidden = NO;
    
    // if you applied a transformation e.g. translate, scale, rotate..., reset to identity
    theView.transform = CGAffineTransformIdentity;
    
    // if you changed the anchor point, this will reset it to the center of the view
    theView.layer.anchorPoint = CGPointMake(0.5, 0.5);
    
    // if you changed the alpha, this will reset it to visible
    theView.alpha = 1.;
    

    【讨论】:

      【解决方案2】:

      试着放线

      [self.view setTransform:aNewTransform];
      

      在完成块中。

      【讨论】:

      • 行不通。 aNewTransform 是假设的,可以是许多不同的变换,具体取决于用户按下的按钮。在我删除动画之前不知道是哪个。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-16
      • 1970-01-01
      • 2019-04-12
      • 2011-04-20
      • 2015-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多