我是ios的新手学习者。我正在使用xcode 6.1。我正在做一个我无法轻易完成的练习。 该任务有一个页面,其中从数组加载两个标签和一个图像视图。该数组包含序列号对应的文本和相应的图像。页面必须先显示“否”,然后等待5秒钟,然后按一下按钮后显示文本和图像。我已经成功完成了任务。但是我想要更进一步。我需要在3秒后显示下一组数组,这对我来说似乎很难完成。我会在这里发布我的代码
f=0
@IBAction func sequential(sender: UIButton) {
var x = self.f
// x is for array index
self.noDispLabel.text = String(i+1)
// Displaying the number
print("\(i)")
// progress bar optional
self.progressBar.setProgress(Float(t.a), animated: true)
// imagearray is the name of the array
var content=self.imageArray[i]
//delay of 5 seconds
var delay = 5.0 * Double(NSEC_PER_SEC)
var time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue())
{
self.imageDisp.image=UIImage(named:content.imageName)
self.titleDispLabel.text=content.imageTitle
self.progressBar.setProgress(0.0, animated: false)
}
self.imageDisp.image=nil
self.titleDispLabel.text=nil
if( self.f < 4) // I have only 4 set of elements in the array
{
self.f++
}
else
{
self.f = 0 // the loop restarts again.
}
}
}
我没有使用核心数据或plist概念。我的图像是资产中的商店。 我知道我正在使用派遣。现在我希望整个块重复数组的所有内容但是我遇到的问题是在完成第一个设置之后主线程循环必须等待3秒才能转到下一个数组元素。有人可以帮助我。
流程应该是这样的
imagearray={ { int no1,string title1,string image1},{ int no2,string title2,string image2},.......... till image4}}
button action
{
for loop till all array elements are visited
{
reset the NoLabel to nil
reset the TitleLabel to nil
reset the ImageView to nil
now display the no1 from imagearray or for loop incrementation (i) in the NoLabel
wait for 5 seconds
display the title1 from imagearray in the TitleLabel
display the image1 from imagearray in the ImageView
now wait for 3 seconds
}// in the next loop no2,title2 and image2 are dispalyed and so on
}
提前致谢。
答案 0 :(得分:0)
如果您需要在5秒内完成第一次处理后3秒内发生某些事情,您可以在功能中添加另外8秒,5 + 3的延迟。
instr = "0123"
outstr = '{0[3]}{0[0]}-{0[2]}{0[1]}'.format(instr)
答案 1 :(得分:0)
检查一下,我已经更新了解决方案......这可能会对你有所帮助
var f = 0
@IBAction func testSequential(sender: UIButton) {
if self.f == self.imageArray.count - 1 {
self.f = 0
}
//delay of 5 seconds
var delayTime = 0.0
for (var j = 0; j < self.imageArray.count; j++)
{
self.imgView.image=nil
self.lblSecond.text=nil
var time = self.delay(delayTime)
dispatch_after(time, dispatch_get_main_queue())
{
self.logTime()
// Displaying the number
self.lblFirst.text = String(self.f+1)
}
delayTime += 5.0
time = self.delay(delayTime)
dispatch_after(time, dispatch_get_main_queue())
{
self.logTime()
self.image(self.imageArray[self.f++])
if self.imageArray.count - 1 == j
{
self.progress.setProgress(Float(0.0), animated: false)
}
}
delayTime += 3.0
}
}
func image(index: Int) {
let progressPer = Float(index)/Float(self.imageArray.count)
self.progress.setProgress(progressPer, animated: true)
let imageName = String(format: "%d", index)
print(imageName)
self.imgView.image=UIImage(named: imageName)
self.lblSecond.text=imageName
}
func delay(var delayTime: Double) -> dispatch_time_t {
delayTime = delayTime * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delayTime))
return time
}
func logTime()
{
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let hour = calendar.components(NSCalendarUnit.Hour, fromDate: date)
let minutes = calendar.components(NSCalendarUnit.Minute, fromDate: date)
let seconds = calendar.components(NSCalendarUnit.Second, fromDate: date)
print("\n \(hour.hour):\(minutes.minute):\(seconds.second)")
}
答案 2 :(得分:0)
经过一些研究后,我找到了答案。 IBAction方法有权仅针对单个用户touchup动作更新UI一次。因此即使在IBAction内部工作for循环也只能生成一个UI更新结果。因此块的迭代部分必须放在IBAction方法之外。该块的结构如下所示。
@IBAction func sequential(sender: UIButton) {
timer = NSTimer.scheduledTimerWithTimeInterval(Double(8), target: self, selector: "updateSequential:", userInfo: nil, repeats: false)
}
var f = 0
func updateSequential(timer2:NSTimer)
{
var x = self.f
// x is for array index
self.noDispLabel.text = String(i+1)
// Displaying the number
print("\(i)")
// progress bar optional
self.progressBar.setProgress(5.0, animated: true)
// imagearray is the name of the array
var content=self.imageArray[i]
//delay of 5 seconds
var delay = 5.0 * Double(NSEC_PER_SEC)
var time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue())
{
self.imageDisp.image=UIImage(named:content.imageName)
self.titleDispLabel.text=content.imageTitle
self.progressBar.setProgress(0.0, animated: false)
}
if( self.f < 4) // I have only 4 set of elements in the array
{
self.f++
}
else
{
self.f = 0 // the loop restarts again.
}
if(self.q==0) // q is a reference variable which is defined on another part of the block to stop the timer.
{
self.timer=NSTimer.scheduledTimerWithTimeInterval(Double(8), target: self, selector: "updateSequential:", userInfo: nil, repeats: false)
}
else
{
[timer.invalidate]
self.timer = nil
}
}
这就是我使用的工作方式。感谢lynda.com分享一个惊人的教程。也适用于stackoverflow其他问题。我忘了备份一个已回答问题的链接,这个问题实际上让我想到了使用NSTimer函数。想知道。