У меня есть рекурсивная функция, которая играет UIView анимации непрерывно (если есть лучший способ сделать это, пожалуйста, дайте мне знать):Стоп рекурсивной функции viewDidUnload
-(void)playAnimationRecursive
{
[appDelegate commenceFishAnimation];
[animationView addSubview:appDelegate.fishAnimationImageView];
animationView.alpha=0.5;
[NSTimer scheduledTimerWithTimeInterval:14.0 target:self selector:@selector(playAnimationRecursive) userInfo:nil repeats:NO];
}
и не уверен, что если это необходимое , но вот AppDelegate commenceFishAnimationMethod:
upperFish=[[UIImageView alloc] initWithImage:upperFishImage];
bigFish=[[UIImageView alloc] initWithImage:bigFishImage];
shark=[[UIImageView alloc] initWithImage:sharkImage];
groupFish=[[UIImageView alloc] initWithImage:groupFishImage];
fish1=[[UIImageView alloc] initWithImage:fish1Image];
fishAnimationImageView=[[[UIImageView alloc] init] retain];
//set the initial position of each fish to be out of frame
upperFish.frame=CGRectMake(-50, 150, 119/2, 93/2); //moves east
bigFish.frame=CGRectMake(-280, 340, 251/2, 137/2); //moves east
shark.frame=CGRectMake(-100, 390, 164/2, 52/2); //moves east
groupFish.frame=CGRectMake(500, 320, 155/2, 89/2); //moves west
fish1.frame=CGRectMake(370, 280, 155/2, 89/2); //moves west
//add fishes to current view
[fishAnimationImageView addSubview:upperFish];
[fishAnimationImageView addSubview:bigFish];
[fishAnimationImageView addSubview:shark];
[fishAnimationImageView addSubview:groupFish];
[fishAnimationImageView addSubview:fish1];
//animate the position of each fish view
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:10.0];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
upperFish.transform=CGAffineTransformMakeTranslation(150, -200);
bigFish.transform=CGAffineTransformMakeTranslation(600, 0);
shark.transform=CGAffineTransformMakeTranslation(550, 0);
groupFish.transform=CGAffineTransformMakeTranslation(-600, 0);
fish1.transform=CGAffineTransformMakeTranslation(-550, 0);
[UIView commitAnimations];
[fishAnimationImageView release];
[upperFish release];
[bigFish release];
[shark release];
[groupFish release];
[fish1 release];
}
так что же происходит, что каждый вид у меня играет эту анимацию в БГ, так что если я на вид а, то он вызывает рекурсивную функцию и воспроизводить анимацию непрерывно , Если я перехожу к просмотру B, он также вызывает рекурсивную функцию и непрерывно воспроизводит анимацию. Тем не менее, я получаю предупреждение об уровне памяти 2, и я думаю, возможно, это потому, что вид A все еще вызывает рекурсивный метод? Если это причина, то как я могу остановить рекурсивный метод при выгрузке? Или это не моя проблема?
EDIT:
Вот мой код сейчас (анимация не цикл)
-(void)commenceFishAnimation
{
UIImageView *newImageView=[[UIImageView alloc] init];
self.fishAnimationImageView=newImageView;
upperFish=[[UIImageView alloc] initWithImage:upperFishImage];
bigFish=[[UIImageView alloc] initWithImage:bigFishImage];
shark=[[UIImageView alloc] initWithImage:sharkImage];
groupFish=[[UIImageView alloc] initWithImage:groupFishImage];
fish1=[[UIImageView alloc] initWithImage:fish1Image];
//set the initial position of each fish to be out of frame
upperFish.frame=CGRectMake(-50, 150, 119/2, 93/2); //moves east
bigFish.frame=CGRectMake(-280, 340, 251/2, 137/2); //moves east
shark.frame=CGRectMake(-100, 390, 164/2, 52/2); //moves east
groupFish.frame=CGRectMake(500, 320, 155/2, 89/2); //moves west
fish1.frame=CGRectMake(370, 280, 155/2, 89/2); //moves west
//add fishes to current view
[fishAnimationImageView addSubview:upperFish];
[fishAnimationImageView addSubview:bigFish];
[fishAnimationImageView addSubview:shark];
[fishAnimationImageView addSubview:groupFish];
[fishAnimationImageView addSubview:fish1];
//animate the position of each fish view
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:10.0];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
upperFish.transform=CGAffineTransformMakeTranslation(150, -200);
bigFish.transform=CGAffineTransformMakeTranslation(600, 0);
shark.transform=CGAffineTransformMakeTranslation(550, 0);
groupFish.transform=CGAffineTransformMakeTranslation(-600, 0);
fish1.transform=CGAffineTransformMakeTranslation(-550, 0);
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(commenceFishAnimation)];
[UIView commitAnimations];
[fishAnimationImageView release];
[upperFish release];
[bigFish release];
[shark release];
[groupFish release];
[fish1 release];
//[newImageView release]; //if I release it, then my animation doesn't play for some reason
}
Тогда в другом классе в методе viewDidLoad, я делаю это:
[appDelegate commenceFishAnimation];
[animationView addSubview:appDelegate.fishAnimationImageView];
Но анимация воспроизводится только один раз.
И вы можете использовать '[UIView setAnimationDelay: 4.0]', чтобы получить 4-секундную паузу между повторами, которые у вас были с вашим методом. – morningstar
Я пробовал то, что вы рекомендовали, но по какой-то причине анимация не зацикливается - она воспроизводится только один раз. У меня есть '[UIView setAnimationDidStopSelector: @selector (commenceFishAnimation)];' в конце моего метода 'commenceFishAnimation'. – Snowman
Я забыл упомянуть, что вам нужно назначить делегата анимации. [UIView setAnimationDelegate: self] Также убедитесь, что вы вызываете его перед вызовом [UIView commitAnimations]; – ender