为了账号安全,请及时绑定邮箱和手机立即绑定

UIView无限360度旋转动画?

UIView无限360度旋转动画?

守着星空守着你 2019-07-31 15:46:34
UIView无限360度旋转动画?我正在尝试旋转UIImageView360度,并在线查看了几个教程。我可以让他们都没有工作,没有UIView停止,或跳到一个新的位置。我怎样才能做到这一点?我尝试过的最新事情是:[UIView animateWithDuration:1.0                       delay:0.0                     options:0                  animations:^{                      imageToMove.transform = CGAffineTransformMakeRotation(M_PI);                  }                   completion:^(BOOL finished){                      NSLog(@"Done!");                  }];但是如果我使用2 * pi,它根本不会移动(因为它是相同的位置)。如果我尝试做pi(180度),它可以工作,但如果我再次调用该方法,它会向后旋转。编辑:[UIView animateWithDuration:1.0                       delay:0.0                     options:0                  animations:^{                      [UIView setAnimationRepeatCount:HUGE_VALF];                      [UIView setAnimationBeginsFromCurrentState:YES];                      imageToMove.transform = CGAffineTransformMakeRotation(M_PI);                  }                   completion:^(BOOL finished){                      NSLog(@"Done!");                  }];也不起作用。它会转到180度数,暂停一瞬间,然后在重新0开始之前重新设置为度数。
查看完整描述

3 回答

?
有只小跳蛙

TA贡献1824条经验 获得超8个赞

找到一个方法(我修改了一下),对我来说非常有效:iphone UIImageView旋转

#import <QuartzCore/QuartzCore.h>- (void) runSpinAnimationOnView:(UIView*)view duration:(CGFloat)duration rotations:(CGFloat)rotations repeat:(float)repeat {
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * rotations * duration ];
    rotationAnimation.duration = duration;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = repeat ? HUGE_VALF : 0;

    [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];}


查看完整回答
反对 回复 2019-07-31
?
慕娘9325324

TA贡献1783条经验 获得超4个赞

我发现他的代码并不是我所需要的。options我相信默认值是给你UIViewAnimationOptionCurveEaseInOut,在连续动画中看起来不正确。此外,我添加了一个检查,以便我可以在需要时(不是无限,但是无限期)在一个偶数四分之一圈停止我的动画,并使加速度在前90度加速,并在最后90度减速(在要求停止后):

// an ivar for your class:BOOL animating;- (void)spinWithOptions:(UIViewAnimationOptions)options {
   // this spin completes 360 degrees every 2 seconds
   [UIView animateWithDuration:0.5
                         delay:0
                       options:options
                    animations:^{
                       self.imageToMove.transform = CGAffineTransformRotate(imageToMove.transform, M_PI / 2);
                    }
                    completion:^(BOOL finished) {
                       if (finished) {
                          if (animating) {
                             // if flag still set, keep spinning with constant speed
                             [self spinWithOptions: UIViewAnimationOptionCurveLinear];
                          } else if (options != UIViewAnimationOptionCurveEaseOut) {
                             // one last spin, with deceleration
                             [self spinWithOptions: UIViewAnimationOptionCurveEaseOut];
                          }
                       }
                    }];}- (void)startSpin {
   if (!animating) {
      animating = YES;
      [self spinWithOptions: UIViewAnimationOptionCurveEaseIn];
   }}- (void)stopSpin {
    // set the flag to stop spinning after one last 90 degree increment
    animating = NO;}

更新

我添加了处理再次开始旋转的请求的能力(startSpin),而之前的旋转正在逐渐减少(完成)。Github上的示例项目。


查看完整回答
反对 回复 2019-07-31
?
慕森卡

TA贡献1806条经验 获得超8个赞

在Swift中,您可以使用以下代码进行无限循环:

斯威夫特4

extension UIView {
    private static let kRotationAnimationKey = "rotationanimationkey"

    func rotate(duration: Double = 1) {
        if layer.animation(forKey: UIView.kRotationAnimationKey) == nil {
            let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation")

            rotationAnimation.fromValue = 0.0
            rotationAnimation.toValue = Float.pi * 2.0
            rotationAnimation.duration = duration
            rotationAnimation.repeatCount = Float.infinity

            layer.add(rotationAnimation, forKey: UIView.kRotationAnimationKey)
        }
    }

    func stopRotating() {
        if layer.animation(forKey: UIView.kRotationAnimationKey) != nil {
            layer.removeAnimation(forKey: UIView.kRotationAnimationKey)
        }
    }}

斯威夫特3

let kRotationAnimationKey = "com.myapplication.rotationanimationkey" // Any keyfunc rotateView(view: UIView, duration: Double = 1) {
    if view.layer.animationForKey(kRotationAnimationKey) == nil {
        let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation")

        rotationAnimation.fromValue = 0.0
        rotationAnimation.toValue = Float(M_PI * 2.0)
        rotationAnimation.duration = duration
        rotationAnimation.repeatCount = Float.infinity

        view.layer.addAnimation(rotationAnimation, forKey: kRotationAnimationKey)
    }}

停止就像:

func stopRotatingView(view: UIView) {
    if view.layer.animationForKey(kRotationAnimationKey) != nil {
        view.layer.removeAnimationForKey(kRotationAnimationKey)
    }}


查看完整回答
反对 回复 2019-07-31
  • 3 回答
  • 0 关注
  • 800 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信