【发布时间】:2014-01-02 13:56:09
【问题描述】:
我有一些代码用于将图像淡入和淡出我的项目,它在另一个项目中时有效,但在主项目中时它给我这个错误?如果它有什么不同,我仍然在我的主项目中使用 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];
}
【问题讨论】:
-
这看起来像是一个无限循环,而不是错误。发布您的代码(不是指向曾经作为您的代码参考的代码的链接)
-
你能发布比截图更多的代码吗……很难阅读。您可以发布代码并添加详细信息以显示错误访问发生的位置......它在什么变量上?
-
我已经添加了代码
-
我删除了所有不相关的代码。问题在于动画停止后调用
nextAnimation,而nextAnimation创建另一个动画。这段代码可能在 4 年前(写博客文章时)有效,但现在不起作用。它只是创建了一个无限循环。我建议找到一种不同的方式来实现您需要的动画。 -
坏建议:将
[self nextAnimation];从viewDidLoad移动到viewDidAppear:。显然,当视图尚未出现时,动画不会发生,并且会立即调用停止选择器。这导致了这个无限循环。
标签: ios objective-c