开发者

Removing Animation from viewDidLoad after Completion

开发者 https://www.devze.com 2023-02-18 15:29 出处:网络
I have a animation (an array of images) that I put in viewDidLoad because I want it to play right after it starts. After the animation in viewDidLoad I also show a webview in an UIWebView which I\'d l

I have a animation (an array of images) that I put in viewDidLoad because I want it to play right after it starts. After the animation in viewDidLoad I also show a webview in an UIWebView which I'd like to stay. The animation plays (for some reason not the UIImage I created) but I can't get rid of it after completion. I've tried putting in a timer and removing the animation from the superview but instead of removing it, it exits the app and goes back to the main page.

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


It looks like the selector you are choosing (removeAnimation:) and the method you are showing (method:) are not named the same.

An easier approach would be to do something like this:

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

Hello, I'm a Panda.


I don't think UIImageView provides delegate callback for when animation stops.

So, if you do it using following question way: How to create a multistage UIImageView animation?

This also removes need for a timer.

Lastly, viewDidLoad is at least documented to be place to setup datamodels and views, as it's not guaranteed to be called at the time you want things animated. I would use "viewWillAppear" or "viewDidAppear" to animate things.

0

精彩评论

暂无评论...
验证码 换一张
取 消