使用addKeyframeWithRelativeStartTime的简单keyFrame动画令人不安

布兰登

我创建了以下keyFrame动画;在这种情况下,常数为UIView的CGPoint x坐标(此动画可以使对象上下移动,重复执行,并且可以平滑地进行移动。

当我将totalDuration设置为时2.0,它可以很好地工作,但是当我将totalDuration设置为1.5或时1.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) {
                              }];
}
丹尼尔·胡德

这样的事情应该起作用:

- (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,而不必更改许多持续时间值。

从文档:(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.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

RoundedImageView有令人不安的角落

来自分类Dev

令人不安的MySQL Join操作

来自分类Dev

令人不安的 irq 将我从睡眠模式中唤醒

来自分类Dev

jQuery:使用:hover的简单动画

来自分类Dev

使用Spritekit的简单Gif动画

来自分类Dev

使用JButton开始简单的动画

来自分类Dev

使用JFrame和JPanel的简单Java动画

来自分类Dev

使用Jquery动画进行简单的CSS更改

来自分类Dev

使用UIView.Animate的简单动画

来自分类Dev

使用网络动画更简单的方法吗?

来自分类Dev

使用JFrame和JPanel的简单Java动画

来自分类Dev

如何使用ngAnimate制作简单的动画

来自分类Dev

通过数组的简单循环,使用“ for”循环代码令人困惑

来自分类Dev

@keyframe动画后,如何更改css属性?

来自分类Dev

CSS @keyframe动画在悬停时闪烁

来自分类Dev

CSS动画不流畅,令人发抖

来自分类Dev

简单for循环的结果令人困惑

来自分类Dev

如何使用onPress的React Native制作简单的动画?

来自分类Dev

如何使用SkiaSharp在Xamarin中创建简单的动画

来自分类Dev

Javascript简单动画循环

来自分类Dev

简单的ImageView颜色动画

来自分类Dev

Java Swing简单动画

来自分类Dev

简单的ImageView颜色动画

来自分类Dev

Javascript简单动画循环

来自分类Dev

组成简单动画

来自分类Dev

给定网站的简单动画

来自分类Dev

SVG简单动画

来自分类Dev

斯威夫特:简单的动画?

来自分类Dev

Unity - 使用简单的精灵动画或简单的粒子效果模拟水流入管道