简体   繁体   English

iOS实时更改View动画速度

[英]iOS change View animation speed in real time

I in my task I have to create animation that starts in switch. 在执行任务时,我必须创建从switch开始的动画。 And I need to change animation speed via slider in real time, without interrupting animation, I think i need to use dispatch, and I`ve stuck on that. 而且我需要通过滑块实时更改动画速度,而不会中断动画,我想我需要使用调度,而我一直坚持下去。

Please help me on example of rotation.Thank you 请帮我举一个旋转的例子。谢谢

@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.");
                 }];
}

I don't know how smooth it would be, but what if you make many small rotations, meaning you would rotate your image only 1 or 2 degrees in a single animation, and in the completion block dispatch the rotatePicture method again (and of course, disable repeating). 我不知道它会变得多么平滑,但是如果您进行许多小旋转,那意味着您将在单个动画中将图像旋转1度或2度,然后在完成块中再次调度rotatePicture方法(当然,请禁用重复)。 That way every animation can start with the updated duration value. 这样,每个动画都可以从更新的持续时间值开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM