views:

105

answers:

1

Hi there, I'm animating a shrinking object. At any point the user can hit a button to get the current scale factor of the object. (I start by scaling the object up using a CGAffineTransformMakeScale, so the scale factor should be 1 when it reaches its original size). I'm just not sure how to retrieve the current scale factor from the animation UIImageView... Here's my code:

- (void)startShrink {
    CGAffineTransform scaleFactor = CGAffineTransformMakeScale(kScaleX, kScaleY);
    imageOutline.transform = scaleFactor;

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

    [imageOutline.layer addAnimation:shrink forKey:@"shrink"];
}

I tried the following, but I'm not sure m12 is the value I should be retrieving from the transform. Or if in fact this is the right approach:

- (float)calculateScale {
    CATransform3D scaleTransform = [(CALayer *)[imageOutline.layer presentationLayer] transform];
    float scale = scaleTransform.m12;
    NSLog(@"Scale: %g", scale);
    return scale;
}

Any advice most appreciated :)

Thanks, Michael.

+1  A: 

If you just scale your view uniformly on all axes then scale value should equal to m11 (and m22 as well).

I just ran a sample with your code copy-pasted - it seems that the following 2 lines are redundant:

CGAffineTransform scaleFactor = CGAffineTransformMakeScale(kScaleX, kScaleY);
imageOutline.transform = scaleFactor;

View immediately shrinks by half (I used 0.5 for scale values) and then shrinks twice more with animation

Vladimir
Great - m11 did the trick, thanks! Those two lines are in order to initially scale the image up by a factor of say 4, so that when the animation is applied, it shrinks from oversize to half its original size.
Smikey