【问题标题】:rotate a UIImageView 360 degrees using block-based使用基于块的 UIImageView 旋转 360 度
【发布时间】:2012-02-23 07:49:09
【问题描述】:

如何使用基于块的动画将 UIImageView 旋转 360 度?

这是我的尝试,但它重复 180 次旋转而不是两个连续的动画

   void (^anim) (void) = ^{
         uiImgDisc.transform = CGAffineTransformMakeRotation(3.14159265);
    };

    void (^after) (BOOL) = ^(BOOL f){
        [UIView animateWithDuration:1
                              delay:0
                            options:UIViewAnimationCurveLinear
                         animations:^(void){
                             uiImgDisc.transform = CGAffineTransformMakeRotation(3.14159265);
                         }
                         completion:nil];

    };

    [UIView animateWithDuration:1
                          delay:0
                        options:UIViewAnimationCurveLinear | UIViewAnimationOptionRepeat
                     animations:anim
                     completion:after];

【问题讨论】:

标签: iphone objective-c core-animation


【解决方案1】:

它不是基于块的,但您可能想尝试使用 CAKeyFrameAnimation 来指定动画需要合并的几个关键帧。与基于块的方法相比的好处是核心动画方法将允许更流畅的动画。

这可能有点矫枉过正,因为正如 Rob 所说,您可以只做一个 CABasicAnimation 并将 fromValue 和 toValue 分别设置为 0 和 2*M_PI。但是这个例子向您展示了如何使用流畅的运动来制作任意旋转动画。

首先,确保将 QuartzCore 框架添加到构建阶段。然后确保使用

导入标头
#import <QuartzCore/QuartzCore.h> 

然后在您的轮换方法中,执行以下操作:

CAKeyframeAnimation *theAnimation = [CAKeyframeAnimation animation];
theAnimation.values = [NSArray arrayWithObjects:
                       [NSValue valueWithCATransform3D:CATransform3DMakeRotation(0, 0,0,1)],
                       [NSValue valueWithCATransform3D:CATransform3DMakeRotation(3.13, 0,0,1)],
                       [NSValue valueWithCATransform3D:CATransform3DMakeRotation(6.26, 0,0,1)],
                       nil];
theAnimation.cumulative = YES;
theAnimation.duration = 1.0;
theAnimation.repeatCount = 0;
theAnimation.removedOnCompletion = YES;


theAnimation.timingFunctions = [NSArray arrayWithObjects:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn], 
                                [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut],
                                nil
                                ];

[imageView.layer addAnimation:theAnimation forKey:@"transform"];

受以下启发:https://github.com/jonasschnelli/UIView-i7Rotate360

祝你好运!

t

【讨论】:

  • 这不是基于块的。此外,如果您要直接使用核心动画,则没有理由使用关键帧动画。只需在 transform.rotation.z 键路径上使用 CABasicAnimation,从 0 开始到 2*M_PI 结束。
  • 它不是基于块的解决方案,但它可以完美运行。谢谢大家的回复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多