动画transform.scale与预期UIViewAnimationOptionBeginFromCurrentState不工作 [英] Animate transform.scale with UIViewAnimationOptionBeginFromCurrentState not working as expected
问题描述
我要的UIView(self.square)的规模财产动画〜10%:
I want to animate scale property of UIView (self.square) to 10%:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[UIView animateWithDuration:2
delay:0.0
options: UIViewAnimationOptionBeginFromCurrentState
animations:^{
self.square.layer.affineTransform = CGAffineTransformMakeScale(0.1f, 0.1f);
}completion:^(BOOL finished){
}];
[self performSelector:@selector(animationInterrupt) withObject:nil afterDelay:1];
}
我想在中间(例如:与用户交互)与动画规模某处中断动画恢复到100%:
I want to interrupt animation somewhere in the middle (e.g.: with user interaction) with animate scale back to 100%:
- (void)animationInterrupt {
[UIView animateWithDuration:1
delay:0.0
options: UIViewAnimationOptionBeginFromCurrentState
animations:^{
self.square.layer.affineTransform = CGAffineTransformMakeScale(1.0f, 1.0f);
}completion:^(BOOL finished){
}];
}
这样的动画产量是很奇怪的。在某些时刻它扩展self.square超过100%:
Output of such animation is very strange. In some moment it scales self.square over 100%:
推荐答案
我发现了以下工作
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 1.0
target: self
selector:@selector(transform)
userInfo: nil repeats:YES];
[timer fire];
}
转换功能检查,如果动画已经在运行,并设置fromvalue到当前值
The transform function checks if the animation is already running and sets the fromvalue to the current value
- (void)transform {
double toValue = 0.1;
if (self.goingUp) toValue = 1.0;
self.goingUp = !self.goingUp;
CATransform3D oldTransform = [(CALayer *)[self.transforemer.layer presentationLayer] transform];
double fromValue = oldTransform.m11;
double duration = toValue - fromValue;
if (fromValue > toValue) duration = fromValue - toValue;
duration *= 2;
CGAffineTransform scaleTransform = CGAffineTransformMakeScale(toValue, toValue);
self.transforemer.transform = scaleTransform;
CABasicAnimation *shrink = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
shrink.toValue = [NSNumber numberWithDouble:toValue];
shrink.fromValue = [NSNumber numberWithDouble:fromValue];
shrink.duration = duration;
shrink.fillMode=kCAFillModeForwards;
shrink.removedOnCompletion=NO;
[self.transforemer.layer addAnimation:shrink forKey:@"transformScale"];
}
在这里看到的输出:
The output seen here:
使用您可以使用code在您的情况最少的努力。
With a minimal effort you can use the code in your situation.
这篇关于动画transform.scale与预期UIViewAnimationOptionBeginFromCurrentState不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!