开发者

UIImageView animation after action?

I have a problem setting up an animation. The first imageview starts animating and when playsound1 is called then starts the second animation. Everything works fine but right now when the second animation stops there is no animation going on. So what I want to do is after the second animation stops - the first to start all over-- then again when method is called to play the second animation . Any hints?

Here you can see a part of the code as it is right now:

- (void)loadtest1 {
    NSArray *imageArray = [[NSArray alloc] initWithObjects:
                           [UIImage imageNamed:@"test1.png"],
                           [UIImage imageNamed:@"test2.png"],
                           [UIImage imageNamed:@"test3.png"],
                           [UIImage imageNamed:@"test4.png"],
                           nil];

    test1.animationImages = imageArray;
    test1.animationRepeatCount = 0;
    test1.animationDuration = 1;

    [imageArray release];
    [test1 startAnimating];
}

- (void)loadtest2 {
    NSArray *imageArray = [[NSArray alloc] initWithObjects:
                           [UIImage imageNamed:@"test4.png"],
                           [UIImage imageNamed:@"test5.png"],
                           [UIImage imageNamed:@"test6.png"],
                           [UIImage imageNamed:@"test7.png"],
                           nil];

    test2.animationImag开发者_如何学Pythones = imageArray;
    test2.animationRepeatCount = -1;
    test2.animationDuration = 1;

    [imageArray release];
    [test2 startAnimating];
}

- (IBAction)playsound1 {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp3"];
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    theAudio.delegate = self;
    [theAudio play];
    test1.hidden = 0;
    test2.hidden = 1;
    [test1 startAnimating];
    test2.center = test1.center;
}


If I understand you correctly, what you are trying to do is re-starting your first animation when the second one finishes

The correct way to handle this is through a CABasicAnimation, which allows you to specify a delegate whose – animationDidStop:finished: method is called when the animation is done.

If you do not want this way, a sort of workaround is the following: in loadtest2, after you start the animation, schedule a method for execution with a delay:

[self performSelector:@selector(loadTest1) withObject:nil afterDelay:1.0];

this will (more or less) work satisfactorily because you know how long your animation will last. So, when the animation should be ended, loadTest1 is executed and the first animation starts again.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜