我在 CAKeyFrameAnimation 上遇到了一个奇怪的问题。
这是代码:
//Inside the selector method of the button
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
[anim setDelegate:self];
anim.path = racetrack.path;
anim.rotationMode = kCAAnimationRotateAuto;
anim.repeatCount = 0;
anim.duration = 10.0;
[carBtn.layer addAnimation:anim forKey:@"race"];
动画效果很好。按下 carBtn(带有汽车背景图像的按钮)时会出现问题。按钮的图像无缘无故旋转了 180°(至少对我来说没有缘由)。创建动画后,我必须使用这行代码自己旋转按钮
carBtn.transform = CGAffineTransformMakeRotation(M_PI);
...但是一两秒钟(在动画开始之前),汽车正对着它的初始位置。按钮是这样创建的:
UIImage* car = [UIImage imageNamed:@"car_image"];
carBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[carBtn setFrame:CGRectMake(startPoint.x-45, startPoint.y-33, macchina.size.width, macchina.size.height)];
[carBtn setBackgroundImage:car forState:UIControlStateNormal];
[carBtn setBackgroundImage:car forState:UIControlStateHighlighted];
[carBtn addTarget:self action:@selector(carPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:carBtn];
这怎么可能?谢谢