题
我有一个相当基本的问题。我有几个(5) UIImageView
图像
我的屏幕。当我单击按钮时,我想要更改每个图像
别的东西。我还希望在每张图像都发生变化时播放声音。
所以我基本上想要在每张图像发生变化时点击声音。
问题是,出于某种原因,如果我说5张图片,
声音首先播放5次,然后图像改变。
这就像图像只有在控制返回时才会刷新 用户输入。
我怎样才能“强迫”一旦我告诉它要显示什么图像,它就刷新图像?
我是iphone / macworld的新手,所以对我很轻松: - )
瑞克
解决方案
就像图像只有在控件返回给用户输入时才会刷新。
这正是发生的事情。 UI在主线程上更新,因此在代码运行时不会发生更新。
为了达到你想要的效果,你需要定时器。看一下NSTimer文档。您需要创建一个计时器,用于触发方法以更改第二个图像,然后重新排列计时器以更改第三个图像。有一个方便的方法来创建你需要的计时器: +(NSTimer *)timerWithTimeInterval:(NSTimeInterval)秒target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)重复代码>
[NSTimer timerWithTimeInterval:1.0f target:self selector:@selector(updateImage2:) userInfo:nil repeats:NO];
然后,你有一个方法:
- (void) updateImage2(NSTimer*)theTimer
{
//Update image 2 here
// play sound 2 here
[NSTimer timerWithTimeInterval:1.0f target:self selector:@selector(updateImage3:) userInfo:nil repeats:NO];
}
我将留给你实现方法updateImage3:; - )
运行这样的更新使主线程有机会更新UI。
其他提示
如果您正在寻找定期刷新UIImageView的方法,可以参考以下内容: http://www.shanison.com/2010/12/ 25 /的UIImageView负载的远程图像/
听起来你可能正在异步播放声音时将图像从磁盘或网络上加载。也许您正在使用 AudioServicesPlaySystemSound
以及 UIImage + imageWithContentsOfURL:
?实际的图像加载和声音播放代码在这里最有用。
当我想改变uiimageview背景并刷新它并在此之后继续执行代码时,这对我有用:
-(void)imgTapped_event:(UIImageView*)imgViewTapped
{
imgViewTapped.backgroundColor=[UIColor whiteColor];
//let the system update ui changes
[NSRunLoop currentRunLoop]runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.0f]];
//continue your code execution, play sounds etc...
//my helper method to play sound
[self playSound:@"goodAnswer" sync:YES loops:0];
}