我想要的是淡化第一张图片并显示下一张图片。我在资源中有3张图片 bundle我能够从第一个图像淡入到下一个然后然后appcrashes在控制台中发出错误..NSInvalidArgumentException- [NSCFString objectAtIndex:]:发送到实例0x5e的无法识别的选择器...下面是代码..你们伙计们帮助我。
标题中的
UIImageView *imageViewBottom, *imageViewTop;
NSArray *imageArray;
实施
int topIndex = 0, prevTopIndex = 1;
-(void)viewDidLoad
{
imageViewBottom = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
[self.view addSubview:imageViewBottom];
imageViewTop = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
[self.view addSubview:imageViewTop];
imageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:@"lori.png"],
[UIImage imageNamed:@"miranda.png"],
[UIImage imageNamed:@"taylor.png"],
[UIImage imageNamed:@"ingrid.png"],
[UIImage imageNamed:@"kasey.png"],
[UIImage imageNamed:@"wreckers.png"], nil];
NSTimer *timer = [NSTimer timerWithTimeInterval:5.0
target:self
selector:@selector(onTimer)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
[timer fire];
[super viewDidLoad];
}
-(void)onTimer
{
if(topIndex %2 == 0)
{
[UIView animateWithDuration:5.0 animations:^
{
imageViewTop.alpha = 0.0;
}];
imageViewTop.image = [imageArray objectAtIndex:prevTopIndex];
imageViewBottom.image = [imageArray objectAtIndex:topIndex];
}
else
{
[UIView animateWithDuration:5.0 animations:^
{
imageViewTop.alpha = 1.0;
}];
imageViewTop.image = [imageArray objectAtIndex:topIndex];
imageViewBottom.image = [imageArray objectAtIndex:prevTopIndex];
}
prevTopIndex = topIndex;
if(topIndex == [imageArray count] - 1)
{
topIndex = 0;
}
else
{
topIndex++;
}
}
答案 0 :(得分:2)
[NSArray arrayWithObjects:...]
已自动释放,因此当您尝试访问时,您正在拾取垃圾
你需要
imageArray = [[NSArray arrayWithObjects:
[UIImage imageNamed:@"lori.png"],
[UIImage imageNamed:@"miranda.png"],
[UIImage imageNamed:@"taylor.png"],
[UIImage imageNamed:@"ingrid.png"],
[UIImage imageNamed:@"kasey.png"],
[UIImage imageNamed:@"wreckers.png"], nil] retain];
不要忘记在dealloc中发布
-(void)dealloc
{
[imageArray release];
[super dealloc];
}
[编辑]关于计时器和评论
尝试使用
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:@selector(onTimer:)
userInfo:nil
repeats:YES];
而不是
NSTimer *timer = [NSTimer timerWithTimeInterval:5.0
target:self
selector:@selector(onTimer)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
[timer fire];
我感觉你的构造以某种方式锁定了线程。我可能错了。
-(void)onTimer:(NSTimer *)aTimer
{
...
}
也是首选签名。