iOS更改视图动画速度实时



i在我的任务中,我必须创建从开关开始的动画。而且我需要实时通过滑块更改动画速度,而不会中断动画,我认为我需要使用discatch,并且我坚持了下来。

请以旋转为例。谢谢

@interface ViewController ()
@property (strong, nonatomic) UIView *currentPicture;
@end
@implementation ViewController
- (void)viewDidLoad {
     [super viewDidLoad];
     self.scaleSwitch.on = self.rotationControl.on = self.translationSwitch.on = NO;
     UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMidX(self.view.bounds)-50,
                                                          150,
                                                          100, 100)];
    myView.backgroundColor = [UIColor redColor];
    [self.view addSubview:myView];
    self.currentPicture = myView;    
}
#pragma mark - Slider Actions
- (IBAction)actionAnimationSpeedSlider:(id)sender {
    self.animationSpeedLabelInfo.text = [NSString stringWithFormat:@"%.3f", self.animationSpeedSlider.value];
}

#pragma mark - Switch Actions
- (IBAction)actionSwitch:(UISwitch *)sender {
    if (self.rotationControl.on)
        [self rotatePicture];
    else
        [sender.layer removeAllAnimations];  
 }

-(void) rotatePicture {
    CGAffineTransform currentPictureState = self.currentPicture.transform;
    [UIView animateWithDuration:self.animationSpeedSlider.value
                      delay:0.3f
                    options:UIViewAnimationOptionCurveLinear|UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionRepeat
                 animations:^{
                     self.currentPicture.transform = CGAffineTransformRotate(currentPictureState, M_PI);
                 }
                 completion:^(BOOL finished) {
                     NSLog(@"Rotation finished!Repeating.");
                 }];
}

我不知道它会有多光滑,但是如果您进行许多小旋转,这意味着您将在一个动画中仅旋转图像1或2度,而在完成块再次调度rotatePicture方法(当然,禁用重复)。这样,每个动画都可以从更新的持续时间值开始。

最新更新