防止 CGAffineTransform 增量应用
我使用 CGAffineTransformRotate 和 CGAffineTransformTranslate 在屏幕上为汽车图像 (UIImageView) 的进度设置动画。动画由 UISlider 控制,因此当用户推进滑块时,汽车就会前进并绕过拐角。对于 UISlider 上的每个点,我需要能够为汽车的 CGAffineTransformRotate 和 CGAffineTransformTranslate 属性设置绝对值。
然而,每次调用变换时,汽车图像都会被增量变换。因此,例如,如果我在滑块点 0.01 处使用它:
carImage.transform = CGAffineTransformRotate(carImage.transform, degreesToRadians(5));
carImage.transform = CGAffineTransformTranslate(carImage.transform, 4.0, - 10.0);
在滑块点 0.02 处使用它:
carImage.transform = CGAffineTransformRotate(carImage.transform, degreesToRadians(10));
carImage.transform = CGAffineTransformTranslate(carImage.transform, 8.0, - 20.0);
当滑块值为 0.02 时,我希望汽车处于 10 度,x 值为 8,y 值为 -20 。然而,我得到的是 15 度的汽车,x 值为 12,y 值为 -30。它将我的第二次转换应用于第一次转换后获得的值。
如何将每个转换应用于我的汽车 UIImageView 的基本值?即我希望每个变换都是基于未变换的 UIImageView 的绝对变换。我不希望转换是增量的。
I'm using CGAffineTransformRotate and CGAffineTransformTranslate to animate the progress of a car image (UIImageView) across the screen. The animation is controlled by a UISlider so as the user advances the slider, the car advances and drives around a corner. For each point on the UISlider, I need to be able to set an absolute value for the car's CGAffineTransformRotate and CGAffineTransformTranslate property.
However, each time the transform is called, the car image is being transformed incrementally. So if, for example, I use this at slider point 0.01:
carImage.transform = CGAffineTransformRotate(carImage.transform, degreesToRadians(5));
carImage.transform = CGAffineTransformTranslate(carImage.transform, 4.0, - 10.0);
and this at slider point 0.02:
carImage.transform = CGAffineTransformRotate(carImage.transform, degreesToRadians(10));
carImage.transform = CGAffineTransformTranslate(carImage.transform, 8.0, - 20.0);
When the slider value is 0.02, I would like the car to be at 10 degrees with an x value of 8 and a y value of -20. However, what I'm getting is the car at 15 degrees with an x value of 12 and a y value of -30. It is applying my second transform to the values arrived at after the first transform.
How can I apply each transform to the basic values of my car's UIImageView? Ie I want each transform to be absolute based on the untransformed UIImageView. I do not want the transforms to be incremental.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看这是否可以解决您的问题,
另一种方式:
您可以保存先前的滑块值,并且当更改滑块时,获取先前和当前滑块值之间的差异,并使用该差异计算相对于先前变换的新变换。例如,如果前一个滑块值为 0.01,当前滑块值为 0.02,则差值为 0.01,因此将 tx 添加 4.0,将 ty 添加 -10。
See if this solve your problem,
Another way:
You can save the previous slider value, and when you change the slider, get the difference between the previous and current slider values, and using that difference calculate the new transformation relative to the previous transformation. For example, if the previous slider value is 0.01 and the current slider value is 0.02, the difference is 0.01 so add 4.0 to the tx and add -10 to ty.