0

我在让 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

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

-(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 点。但是,当触发动画时,我的视图开始移动,但动画似乎在视图向右移动 250 点之前结束,导致 UIView '传送'到其最终位置。我似乎无法弄清楚是什么导致了这种行为。

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

任何帮助和建议表示赞赏。

4

3 回答 3

1
  • 像这样试试

CABasicAnimation *旋转动画;

    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"];
于 2013-08-08T10:35:43.233 回答
0

如果你的目标是移动你的视图,你的代码应该是这样的:

CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"position"];
[animation setFromValue:[NSValue valueWithCGPoint:self.view.layer.position]];
CGPoint p = self.view.layer.position;
p.x += 250;
self.view.layer.position = p;
[animation setDuration:1];
[animation setRemovedOnCompletion:NO];
[self.view.layer addAnimation:animation forKey:@"toggleMenu"];

你真的应该改变你的视图(层)的位置。否则,当您的动画被移除时,您的视图将保持在之前的位置。UIView Animation这就是和之间的区别Core Animation

于 2013-08-08T11:21:32.940 回答
0
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"position"];
[animation setFromValue:[NSValue valueWithCGPoint:view.layer.position]];
CGPoint newPosition = CGPointMake(200, 300);
view.layer.position = p;
[animation setDuration:0.5f];
[animation setRemovedOnCompletion:NO];
[group setFillMode:kCAFillModeForwards];
[[view layer] addAnimation:animation forKey:@"position"];
于 2015-08-13T12:43:31.157 回答