我目前正在開發iPhone應用程序,而且我正在努力製作一些動畫。基於觸摸的iPhone動畫
基本上我有109幀用於在屏幕上拖動一個人,所以我正在做的是傾聽觸摸,並計算我需要切換到哪些幀。
這工作都很好,但過了一段時間後,我得到一個內存泄漏,應用程序崩潰。我在啓動時將所有圖像加載到數組中,並使用UIImageView
來顯示圖像。圖像使用imageWithContentsOfFile:
加載。
什麼是我最好的辦法呢?
繼承人一些源代碼:
- (void)viewWillAppear:(BOOL)animated {
animationQueue = [[NSMutableArray alloc] initWithObjects:0];
imageArray = [[NSMutableArray alloc] initWithObjects:nil];
for(int i = 1;i<110;i++)
{
[imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat: @"%i", i] ofType:@"png"]]];
}
[super viewWillAppear:animated];}
查看有沒有加載代碼:
- (void)viewDidLoad {
animation = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 480, 320)];
animation.contentMode = UIViewContentModeCenter;
[self.view addSubview:animation];
animation.image = [imageArray objectAtIndex:40];
[super viewDidLoad];}
潤色處理程序:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
// Enumerates through all touch objects
for (UITouch *touch in touches) {
CGPoint touch_point = [touch locationInView:self.view];
// calculate which frame to end on
int pos = ceil((touch_point.x/480) * 108);
[self redrawAnimation:pos];
}}
重繪動畫:
- (void)redrawAnimation:(int)end_frame {
animation.image = [imageArray objectAtIndex:end_frame];
}
當您沿着屏幕滑動手指幾次時,應用程序崩潰。
你能發佈一些你的代碼,並告訴我們點,應用程序崩潰? – schaechtele 2010-04-07 09:51:58
當您沿着屏幕滑動手指幾次時,應用程序崩潰。 – user310729 2010-04-07 12:23:21