我正在尝试做一个简单的动画,当一个按钮被点击时,它会连续旋转图像,但由于某种原因,当我点击按钮时,它会使应用程序崩溃。
这是我的代码。
.h文件:
@interface MainView : UIView {
}
IBOutlet UIImageView *sunray;
- (IBAction)pushrotate:(id)sender;
@end.m文件:
@implementation MainView
- (IBAction)pushrotate:(id)sender {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0];
sunray.transform = CGAffineTransformMakeRotation(6.2831855);
[UIView commitAnimations];
}
-(void) dealloc {
[sunray dealloc];
[sunray release];
[super dealloc];
}
@end你知道我怎么才能让它工作吗?另外,让动画在应用程序加载后立即开始的最好方法是什么?
发布于 2011-03-29 19:15:14
如果您将视图旋转角度设置为2*PI的倍数(在您的情况下看起来是这样),则视图会认识到变换实际上没有效果,并且视图不会旋转。
要使用动画旋转视图的角度超过2*PI,您需要使用CoreAnimation。所以你的代码应该看起来像这样:
//link with QuartzCore.framework
#import <QuartzCore/QuartzCore.h>
...
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
animation.duration = 1.0f;
[sanray.layer addAnimation:animation forKey:@"MyAnimation"];发布于 2011-03-29 19:57:34
您可能连接了错误的操作或插座(如果您发布了更多的控制台输出,我们可以肯定)。查看Interface Builder在您的操作/出口上是否有任何错误标记。
另请参阅我关于自己调用dealloc的评论-你不应该这样做,除非在你自己的dealloc实现中调用super。
发布于 2012-11-16 07:32:05
以下是我旋转图像的解决方案
-(void)rotate:(int)degree {
float rad = M_PI * (float)degree / 180.0;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
aktivImageView.transform = CGAffineTransformMakeRotation(rad);
[UIView commitAnimations];
}我想你也可以把这个代码片段和一个按钮一起使用
在你的IBAction中,你可以编码self rotate:90;给出90°的等级,或者你可以使用self rotate:0;不旋转你的图像;-)
https://stackoverflow.com/questions/5471528
复制相似问题