我有一个非常简单的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"];
答案 0 :(得分:2)
在现有视图上开始新动画之前,如果先前已更改任何这些属性,您可以重置视图:
// 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.;
答案 1 :(得分:0)
尝试放行
[self.view setTransform:aNewTransform];
在完成块中。