gpt4 book ai didi

ios - 使用 Core Animation 为 UIView 制作动画?

转载 作者:行者123 更新时间:2023-11-29 03:43:05 25 4
gpt4 key购买 nike

我在使用核心动画让 UiView 正确设置动画时遇到了一些麻烦。这是我的代码:

CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"position"];
[animation setFromValue:[NSValue valueWithCGPoint:self.view.layer.position]];
[animation setToValue:[NSValue valueWithCGPoint:CGPointMake(250, self.view.layer.position.y)]]; //I want to move my UIView 250 pts to the right
[animation setDuration:1];
[animation setDelegate:self]; //where self is the view controller of the view I want to animate
[animation setRemovedOnCompletion:NO];
[self.view.layer addAnimation:animation forKey:@"toggleMenu"]; //where self.view returns the view I want to animate

我还实现了以下委托(delegate)方法:

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
if(flag)
[self.view.layer setTransform:CATransform3DMakeTranslation(250, 0, 0)]; //set layer to its final position
}

我正在尝试使 UIView 向右移动 250 点。但是,当触发动画时,我的 View 开始移动,但动画似乎在 View 向右移动 250 点之前结束,导致 UIView“传送”到其最终位置。我似乎无法弄清楚是什么导致了这种行为。

我还尝试使用 UIView 方法 +(void)animateWithDuration:animations: 并且这种方法效果很好。但是,我正在尝试实现微妙的“反弹”效果,并且我宁愿使用 setTimingFunction:functionWithControlPoints: 方法来实现它,而不是使用多个回调。

感谢任何帮助和建议。

最佳答案

  • 尝试这样

CABasicAnimation *rotationAnimation;

    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"position"];

rotationAnimation.fromValue=[NSValue valueWithCGPoint:CGPointMake([view center].x, [view center].y)];
rotationAnimation.toValue=[NSValue valueWithCGPoint:CGPointMake([view center].x+250, [view center].y)];

rotationAnimation.duration = 1.0+i;
rotationAnimation.speed = 3.0;

rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 1.0;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];

[view.layer addAnimation:rotationAnimation forKey:@"position"];

关于ios - 使用 Core Animation 为 UIView 制作动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18121825/

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