[英]Why Won't My Image Rotate in Xcode?
我正在嘗試做一個簡單的動畫,當單擊一個按鈕時,它將連續不斷地旋轉圖像,但是由於某些原因,當我單擊該按鈕時,它會使應用程序崩潰。
這是我的代碼。
.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
知道如何使它起作用嗎? 此外,使動畫在應用程序加載后立即開始的最佳方法是什么?
這是我旋轉圖像的解決方案
-(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中,您可以編碼[自我旋轉:90]; 以90°進行評級,或者您可以使用[自我旋轉:0]; 不旋轉圖像;-)
如果將視圖旋轉角度設置為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"];
您可能將操作或插座連接錯誤(如果您發布了更多控制台輸出,我們將確定)。 查看Interface Builder在您的操作/出口上是否有任何錯誤標記。
另請參閱我關於自己調用dealloc的評論-您不應該這樣做,除了您自己的dealloc實現中的[super dealloc]外。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.