【问题标题】:Animate UIView using Core Animation?使用核心动画为 UIView 设置动画?
【发布时间】:2013-08-08 08:55:59
【问题描述】:

让 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: 方法来实现它,而不是使用多个回调。

感谢任何帮助和建议。

【问题讨论】:

  • 对于反弹,您可以尝试在位置上使用关键帧动画。

标签: ios animation uiview core-animation


【解决方案1】:
  • 这样试试

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

【讨论】:

    【解决方案2】:

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

    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 AnimationCore Animation 之间的区别。

    【讨论】:

      【解决方案3】:
      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"];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-23
        • 1970-01-01
        • 2010-12-25
        相关资源
        最近更新 更多