knagode
knagode

Reputation: 6125

Animate transform.scale with UIViewAnimationOptionBeginFromCurrentState not working as expected

I want to animate scale property of UIView (self.square) to 10%:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [UIView animateWithDuration:2
                      delay:0.0
                    options: UIViewAnimationOptionBeginFromCurrentState
                 animations:^{
                     self.square.layer.affineTransform = CGAffineTransformMakeScale(0.1f, 0.1f);
                 }completion:^(BOOL finished){

                 }];
    [self performSelector:@selector(animationInterrupt) withObject:nil afterDelay:1];
}

I want to interrupt animation somewhere in the middle (e.g.: with user interaction) with animate scale back to 100%:

- (void)animationInterrupt {
   [UIView animateWithDuration:1
                      delay:0.0
                    options: UIViewAnimationOptionBeginFromCurrentState
                 animations:^{
                     self.square.layer.affineTransform =  CGAffineTransformMakeScale(1.0f, 1.0f);
                 }completion:^(BOOL finished){

                 }];
}

Output of such animation is very strange. In some moment it scales self.square over 100%:

enter image description here

Upvotes: 0

Views: 1198

Answers (1)

rcresnik
rcresnik

Reputation: 120

I found that the following works

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 1.0
                                                      target: self
                                                    selector:@selector(transform)
                                                    userInfo: nil repeats:YES];
    [timer fire];
}    

The transform function checks if the animation is already running and sets the fromvalue to the current value

- (void)transform {
    double toValue = 0.1;
    if (self.goingUp) toValue = 1.0;
    self.goingUp = !self.goingUp;
    CATransform3D oldTransform = [(CALayer *)[self.transforemer.layer presentationLayer] transform];
    double fromValue = oldTransform.m11;

    double duration = toValue - fromValue;
    if (fromValue > toValue) duration = fromValue - toValue;
    duration *= 2;

    CGAffineTransform scaleTransform = CGAffineTransformMakeScale(toValue, toValue);
    self.transforemer.transform = scaleTransform;

    CABasicAnimation *shrink = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    shrink.toValue = [NSNumber numberWithDouble:toValue];
    shrink.fromValue = [NSNumber numberWithDouble:fromValue];
    shrink.duration = duration;
    shrink.fillMode=kCAFillModeForwards;
    shrink.removedOnCompletion=NO;

    [self.transforemer.layer addAnimation:shrink forKey:@"transformScale"];
}

The output seen here:

With a minimal effort you can use the code in your situation.

Upvotes: 1

Related Questions