gpt4 book ai didi

ios - 结束 UIView 动画时将 View 返回到原始位置

转载 作者:行者123 更新时间:2023-11-28 23:12:37 25 4
gpt4 key购买 nike

我有一个非常简单的 UIView 动画,它使我的 View “跳动”:

[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%。

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

编辑 2:我找到了一个解决方案,使用 CABasicAnimation 而不是 UIView 动画。如果有人找到使用 UIView 动画的解决方案,我仍然会感兴趣...我更喜欢基于 block 的界面。这也只有效,因为我碰巧跟踪我的比例值与应用于 View 的比例值分开。任何改变比例的东西都使用一种同样改变 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"];

最佳答案

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

// 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.;

关于ios - 结束 UIView 动画时将 View 返回到原始位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7774215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com