gpt4 book ai didi

ios - 动画 transform.scale 与 UIViewAnimationOptionBeginFromCurrentState 不按预期工作

转载 作者:行者123 更新时间:2023-11-29 02:34:12 25 4
gpt4 key购买 nike

我想将 UIView (self.square) 的缩放属性设置为 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%:

- (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% 以上:

enter image description here

最佳答案

我发现以下内容有效

- (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];
}

transform 函数检查动画是否已经在运行并将 fromvalue 设置为当前值

- (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"];
}

此处看到的输出:

只需付出最少的努力,您就可以在您的情况下使用该代码。

关于ios - 动画 transform.scale 与 UIViewAnimationOptionBeginFromCurrentState 不按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26543763/

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