Getting the scale factor from a CABasicAnimation
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 = CGAffineTransformMakeScal开发者_Python百科e(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.
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
精彩评论