gpt4 book ai didi

ios - 为什么图像过渡会影响我的动画?

转载 作者:行者123 更新时间:2023-12-01 18:58:21 25 4
gpt4 key购买 nike

我在 ImageView 上不断运行动画:

[UIView animateWithDuration:dur
delay:curAnim.delay
options:(params)
animations:^{
self.view.layer.opacity = curAnim.opacity;
self.view.center = curAnim.center;
CGAffineTransform tr = CGAffineTransformMakeRotation(curAnim.rotation);
tr = CGAffineTransformScale(tr, curAnim.scale, curAnim.scale);
self.view.transform = tr;

}
completion:^(BOOL finished){

}];

然后,随着恒定动画的运行,我想转换到另一个图像:
-(void)transitionToImage:(UIImage*)image duration:(CGFloat)duration
{
UIImageView* iv = (UIImageView*)self.view;

iv.image = image;

CATransition *transition = [CATransition animation];
transition.duration = duration;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionFade;

[iv.layer addAnimation:transition forKey:@"transition"];
}

但是,当我转换时,现在有 2 个 View ,一个移动,一个不移动,而不是在我的 ImageView 上发生转换。

我尝试了各种过渡方法,但都产生了相同的问题。

我怎样才能做到这一点,以便我可以过渡并继续当前正在运行的动画?

最佳答案

对两个动画都使用 Core Animation

CABasicAnimation *posAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
posAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(250, 250)];
posAnimation.fromValue = [NSValue valueWithCGPoint:self.imageView.layer.position];
posAnimation.duration = 3;
posAnimation.fillMode = kCAFillModeForwards;
posAnimation.removedOnCompletion = NO;
[self.imageView.layer addAnimation:posAnimation forKey:nil];

CABasicAnimation *imgAnim = [CABasicAnimation animationWithKeyPath:@"contents"];
imgAnim.fromValue = (id)[UIImage imageNamed:@"icon1"].CGImage;
imgAnim.toValue = (id)[UIImage imageNamed:@"icon2"].CGImage;
imgAnim.duration = 3;
imgAnim.fillMode = kCAFillModeForwards;
imgAnim.removedOnCompletion = NO;
[self.imageView.layer addAnimation:imgAnim forKey:nil];

CoreAnimation (CATransition) 并没有真正改变属性。 [UIView animateWithDuration: ...] 确实如此。所以 CATransition 不关心当前位置在哪里。它会在动画开始的位置抓取您的图像,并仅在始终位于同一位置的“presentationLayer”(而不是图层本身)上执行转换。

更新:
CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnim.fromValue = @1;
scaleAnim.toValue = @2;
scaleAnim.duration = 3;
scaleAnim.fillMode = kCAFillModeForwards;
scaleAnim.removedOnCompletion = NO;
[self.imageView.layer addAnimation:scaleAnim forKey:nil];


CABasicAnimation *rotAnim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rotAnim.fromValue = @0;
rotAnim.toValue = @(M_PI);
rotAnim.duration = 3;
rotAnim.fillMode = kCAFillModeForwards;
rotAnim.removedOnCompletion = NO;
[self.imageView.layer addAnimation:rotAnim forKey:nil];

关于ios - 为什么图像过渡会影响我的动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24872507/

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