完成后从viewDidLoad中删除动画

时间:2011-03-17 19:45:50

标签: iphone objective-c ios animation

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

-(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];
}

2 个答案:

答案 0 :(得分: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];
}

你好,我是熊猫。

答案 1 :(得分:0)

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

因此,如果您使用以下问题方式执行此操作:   How to create a multistage UIImageView animation?

这也消除了对计时器的需求。

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