使用addKeyframeWithRelativeStartTime的简单keyFrame动画是紧张不安的

时间:2014-10-12 20:37:56

标签: objective-c cakeyframeanimation

我创建了以下keyFrame动画;在这种情况下,常量是UIView的CGPoint x坐标(此动画上下移动对象,重复,据说是顺畅的。

当我将totalDuration设置为2.0时,它可以正常工作,但是当我将totalDuration设置为1.51.6时,它会变得一团糟 - 好像UIView没有时间在动画再次开始之前返回原来的位置。

显然我的数学错误,但我无法将手指放在哪里。

- (void)linearAnimation:(CGFloat)constant imageView:(UIImageView*)imageView animationStyle:(RSGesturesTutorialAnimationStyle)animationStyle{
    CGFloat frameDuration;
    CGFloat totalDuration = 1.6; // setting this to 2.0 animates smoothly, 1.5/6 do not.

    NSArray* keyFrames = @[
                          [NSNumber numberWithFloat:constant - 100],
                          [NSNumber numberWithFloat:constant],
                          [NSNumber numberWithFloat:constant + 100],
                          [NSNumber numberWithFloat:constant]
                          ];

    frameDuration = totalDuration/keyFrames.count;

    [UIView animateKeyframesWithDuration:totalDuration
                                   delay:0
                                 options: UIViewKeyframeAnimationOptionCalculationModeLinear | UIViewKeyframeAnimationOptionRepeat
                              animations:^{
                                  [keyFrames enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                                      [UIView addKeyframeWithRelativeStartTime:idx*frameDuration relativeDuration:frameDuration animations:^{
                                          imageView.center = CGPointMake(imageView.center.x, [obj floatValue]);
                                      }];
                                  }];
                              } completion:^(BOOL finished) {
                              }];
}

1 个答案:

答案 0 :(得分:2)

这样的事情应该有效:

- (void)linearAnimation:(CGFloat)constant imageView:(UIImageView *)imageView {
    NSTimeInterval totalDuration = 1.6; // setting this to 2.0 animates smoothly, 1.5/6 do not.
    CGFloat startY = imageView.center.y;

    NSArray* keyFrames = @[
                         @(startY - constant),
                         @(startY),
                         @(startY + constant),
                         @(startY)
                         ];

    CGFloat period = 1.0f / keyFrames.count;

    [UIView animateKeyframesWithDuration:totalDuration
                               delay:0
                             options: UIViewKeyframeAnimationOptionCalculationModeLinear | UIViewKeyframeAnimationOptionRepeat
                          animations:^{

                              [keyFrames enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

                                  NSTimeInterval interval = idx * period;

                                  [UIView addKeyframeWithRelativeStartTime:interval relativeDuration:period animations:^{

                                      imageView.center = CGPointMake(imageView.center.x, [obj floatValue]);
                                  }];
                              }];
                          } completion:^(BOOL finished) {
                          }];
}

向动画添加关键帧时,relativeStartTimerelativeDuration值不是绝对值,它们必须是整个动画的百分比。这样您就可以更改totalDuration,而无需更改许多持续时间值。

来自doc:(frameStartTime) The time at which to start the specified animations. This value must be in the range 0 to 1, where 0 represents the start of the overall animation and 1 represents the end of the overall animation. For example, for an animation that is two seconds in duration, specifying a start time of 0.5 causes the animations to begin executing one second after the start of the overall animation.