从 CABasicAnimation 获取比例因子

发布于 2024-09-14 12:21:50 字数 1000 浏览 1 评论 0原文

我正在制作一个缩小的物体的动画。用户可以随时点击按钮来获取对象的当前比例因子。 (我首先使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青巷忧颜 2024-09-21 12:21:50

如果您只是在所有轴上均匀缩放视图,则缩放值应等于 m11(以及 m22)。

我刚刚运行了一个示例,其中复制粘贴了您的代码 - 似乎以下两行是多余的:

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

视图立即缩小一半(我使用 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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文