从 CABasicAnimation 获取比例因子
我正在制作一个缩小的物体的动画。用户可以随时点击按钮来获取对象的当前比例因子。 (我首先使用 CGAffineTransformMakeScale 放大对象,因此当它达到原始大小时,比例因子应该为 1)。我只是不确定如何从动画 UIImageView 中检索当前比例因子...这是我的代码:
- (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"];
}
我尝试了以下操作,但我不确定 m12 是我应该从转换中检索的值。或者,如果事实上这是正确的方法:
- (float)calculateScale {
CATransform3D scaleTransform = [(CALayer *)[imageOutline.layer presentationLayer] transform];
float scale = scaleTransform.m12;
NSLog(@"Scale: %g", scale);
return scale;
}
任何建议都非常感激:)
谢谢,迈克尔。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只是在所有轴上均匀缩放视图,则缩放值应等于 m11(以及 m22)。
我刚刚运行了一个示例,其中复制粘贴了您的代码 - 似乎以下两行是多余的:
视图立即缩小一半(我使用 0.5 作为比例值),然后用动画再缩小两倍
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:
View immediately shrinks by half (I used 0.5 for scale values) and then shrinks twice more with animation