How to let a view rotate forever?

前端 未结 3 1288
执念已碎
执念已碎 2021-01-13 06:49

Is there a way to let a view rotate forever, with an specified speed? I need that for an indicator kind of thing. I know there is this weird Lxxxxx00ff constant (don\'t reme

3条回答
  •  梦毁少年i
    2021-01-13 07:38

    my solution for this is a bit hacky since it doesn't use core animation but at least it works truly forever and doesn't require you to setup multiple animation steps.

    ...
    // runs at 25 fps
    NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:1.0/25
                              target:self
                              selector:@selector(rotate)
                              userInfo:nil
                              repeats:YES];
    [timer fire];
    ...
    
    - (void)rotate {
        static int rotation = 0;
    
        // assuming one whole rotation per second
        rotation += 360.0 / 25.0;
        if (rotation > 360.0) {
            rotation -= 360.0;
        }
        animatedView.transform = CGAffineTransformMakeRotation(rotation * M_PI / 180.0);
    }
    

提交回复
热议问题