2011-03-17 58 views
1

我有一个动画(一个图像数组),我把它放在viewDidLoad中,因为我希望它在它启动后立即播放。在viewDidLoad中的动画后,我也显示了一个UIWebView中的webview,我想留下来。动画播放(出于某种原因,不是我创建的UIImage),但完成后我无法摆脱它。我尝试过放入计时器并从超级视图中移除动画,但不是将其移除,而是退出应用程序并返回主页面。完成后从viewDidLoad中删除动画

-(void)viewDidLoad 
{ 
    UIImage *first = [UIImage imageNamed:@"panda1.png"]; 
    animation = [[UIImageView alloc] initWithImage:first]; 
    animation.animationImages = [NSArray arrayWithObjects: first, 
            [UIImage imageNamed:@"panda2.png"], 
            [UIImage imageNamed:@"panda3.png"], nil]; 

    animation.animationRepeatCount = 4; 
    animation.animationDuration = 2; 
    [animation startAnimating]; 

    [self.view addSubview:animation]; 

    //set timer to remove animation from view 
    NSTimer *timer; 
    timer = [NSTimer scheduledTimerWithTimeInterval: 3 
          target: self 
         selector: @selector(removeAnimation:) 
         userInfo: nil 
         repeats: NO]; 

    //for loading the image at start up 
    NSString *urlAddress = @"http://projects.seng.uvic.ca/most_recent.php"; 
    NSURL *url = [NSURL URLWithString:urlAddress]; 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 

    //load the request in the UIWebView 
    [webView loadRequest:requestObj]; 

}

//removes the animation from subview 
- (void) method: (NSTimer*) theTimer 
{ 
    [animation release]; 
    [animation removeFromSuperview]; 
} 

回答

1

它看起来像您选择的选择器(removeAnimation:)和你呈现的方法(method:)未命名一样。

一种更简单的方法是做这样的事情:

-(void)viewDidLoad 
{ 
    UIImage *first = [UIImage imageNamed:@"panda1.png"]; 
    animation = [[UIImageView alloc] initWithImage:first]; 
    animation.animationImages = [NSArray arrayWithObjects: first, 
            [UIImage imageNamed:@"panda2.png"], 
            [UIImage imageNamed:@"panda3.png"], nil]; 

    animation.animationRepeatCount = 4; 
    animation.animationDuration = 2; 
    [animation startAnimating]; 

    [self.view addSubview:animation]; 
    [self performSelector:@selector(removeAnimation) withObject:nil afterDelay:8.0]; 


    //for loading the image at start up 
    NSString *urlAddress = @"http://projects.seng.uvic.ca/most_recent.php"; 
    NSURL *url = [NSURL URLWithString:urlAddress]; 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 

    //load the request in the UIWebView 
    [webView loadRequest:requestObj]; 
} 

- (void)removeAnimation 
{ 
    [animation removeFromSuperview]; 
    [animation release]; 
} 

你好,我是一个熊猫。

+0

oops完全没有注意到缺少的函数名称(仍然习惯这个mac键盘:()添加一个函数名称解决了我的问题。 – panda 2011-03-18 18:44:02

0

我不认为UIImageView为动画停止时提供委托回调。

所以,如果你这样做使用以下问题的方式: How to create a multistage UIImageView animation?

这也消除需要一个计时器。

最后,viewDidLoad至少被记录为可以设置数据模型和视图的地方,因为它不能保证在你想要动画的时候被调用。我会使用“viewWillAppear”或“viewDidAppear”来动画。