动画图像时无限循环

时间:2014-01-02 13:56:09

标签: ios objective-c

我有一些代码我正在使用淡入淡出我的项目的图像它在另一个项目中工作但是当它在主项目中它给我这个错误?如果它有任何区别我仍然在我的主项目中使用xib但我正在测试项目中使用故事板。我已经上传了一个屏幕截图,并在网站上附上了代码,所以希望它可能对你们更有意义。

http://alohacocoa.com/post/293647216/iphone-animation-fade-between-multiple-images

主题1:EXC_BAD_ACCESS(代码= 2,地址= 0xbf7fffdc)

https://pbs.twimg.com/media/Bc-r-LdCYAABd1O.jpg:large

- (void)viewDidLoad
{
    [super viewDidLoad];
    backgroundImageQueue = [[NSMutableArray alloc] init];
    [backgroundImageQueue addObject:
     [UIImage imageNamed:@"image1.jpg"]];
    [backgroundImageQueue addObject:
     [UIImage imageNamed:@"image2.jpg"]];

    /* Add any more images to the queue */
    backgroundB.image = [backgroundImageQueue
                         objectAtIndex:[backgroundImageQueue count] - 1];
    [backgroundImageQueue insertObject:
     backgroundB.image atIndex:0];
    [backgroundImageQueue removeLastObject];
    backgroundA.alpha = 1.0;
    backgroundB.alpha = 0.0;
    [self nextAnimation];
}

-(void)nextAnimation {
    backgroundA.image = backgroundB.image;
    backgroundB.image = [backgroundImageQueue
                         objectAtIndex:[backgroundImageQueue count] - 1];
    [backgroundImageQueue insertObject:
     backgroundB.image atIndex:0];
    [backgroundImageQueue removeLastObject];
    backgroundA.alpha = 1.0;
    backgroundB.alpha = 0.0;

    [UIView beginAnimations:@"fade" context:NULL];
    [UIView setAnimationDuration:6];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:
     @selector(nextAnimation)];
    backgroundA.alpha = 0.0;
    backgroundB.alpha = 1.0;
    [UIView commitAnimations];
}

1 个答案:

答案 0 :(得分:0)

你应该能够看到你的堆栈,它会说两件事之一:

viewDidLoad
nextAnimation
(maybeSomethingElse)
viewDidLoad
nextAnimation
(maybeSomethingElse)
viewDidLoad
nextAnimation
(maybeSomethingElse)
viewDidLoad
nextAnimation
...

viewDidLoad
nextAnimation
(maybeSomethingElse)
nextAnimation
(maybeSomethingElse)
nextAnimation
(maybeSomethingElse)
nextAnimation
(maybeSomethingElse)
nextAnimation
...

它将为您提供有关正在发生的事情的线索,是一遍又一遍地加载视图,或者是递归调用的下一个动画。

但我认为@matthiasBauch是正确的,你需要将[self nextAnimation]移到视图周期的后期