【发布时间】:2010-05-09 16:18:03
【问题描述】:
我正在为 iphone 应用程序编写仪表显示:针对固定背景 UIImageView 的居中针 UIImageView。我是 iPhone 编程的新手,对 CoreAnimation 了解不多,但是通过查看示例并摆弄我的 anchorPoint 设置和针头图像在 InterfaceBuilder 中的背景位置,我的针头想出了我想要的旋转点在背景中正确居中:
self.needleIV.layer.anchorPoint = CGPointMake( 0.5, 0.68 );
我的测试动画完美运行(指针在仪表中保持正确居中,并围绕我想要的旋转点,锚点旋转),使用以下代码:
CABasicAnimation *rotateAnimation = [CABasicAnimation animation];
rotateAnimation.keyPath = @"transform.rotation.z";
rotateAnimation.fromValue = [NSNumber numberWithFloat:DegreesToRadians( (rand() % 270 - 135) )];
rotateAnimation.toValue = [NSNumber numberWithFloat:DegreesToRadians( (rand() % 270 - 135) )];
rotateAnimation.duration = 4.0;
rotateAnimation.removedOnCompletion = NO;
// leaves presentation layer in final state; preventing snap-back to original state
rotateAnimation.fillMode = kCAFillModeBoth;
rotateAnimation.repeatCount = 0;
rotateAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
// Add the animation to the selection layer. This causes it to begin animating.
[self.needleIV.layer addAnimation:rotateAnimation forKey:@"rotateAnimation"];
问题在于,在应用程序启动时,针最初指向上方(这是针在图像文件中的外观),我不知道如何给它一个初始旋转值,而不是弄乱动画代码。 (“搞砸”是指指针没有在正确的轴上旋转,并且在仪表中变得不居中)。
我尝试将 layer.transform 设置为围绕 z 轴旋转的 3d 旋转矩阵,但这会使指针偏离中心。我也试过这段代码:
[self.needleIV.layer setValue:[NSNumber numberWithFloat:DegreesToRadians(-135.0)] forKeyPath:@"transform.rotation.z"];
这也不起作用(针从锚点中心移开),我寄予厚望,因为它显然对工作动画代码使用了相同的转换。
我还尝试将工作动画 sn-p 放在我的视图控制器的 viewDidLoad 函数中,以将针“动画化”到它的起始位置,但它根本没有做任何事情——显然动画直到图像实际上在屏幕上,并且我已经在调试器中确认 viewDidLoad 中屏幕仍然是黑色的。
此时,我将尝试在应用程序开始设置正确旋转针的动画后,在计时器中运行“足够长”的初始动画,但我真的很想了解发生了什么这里错了,以及为什么动画显然围绕不同的轴旋转,而不是我在预旋转/预动画方面的其他尝试。
【问题讨论】:
标签: iphone