使用 NSTimer 快速查看进度 [英] Swift Progress View with NSTimer
问题描述
我有一个进度视图栏,我想用它来指示时间.这是我在 Swift 中的第一个项目,我不确定如何去做.所以任何帮助/建议将不胜感激...
I have a Progress view bar that I would like to use to indicate time. This is my first project in Swift, and I am unsure how to go about this. So any help/ advise would be appreciated ...
(使用 Xcode 7.2 和 Swift 2.0)
(Using Xcode 7.2 and Swift 2.0)
下面是我的视图控制器.当btnPlaySession"被触发时,视图控制器上的内容每 20 秒改变一次.当计时器计数到 20 时,id 喜欢用进度条来指示这一点(因此每次内容更改时,进度条都会重置).
Below Is my view controller. When 'btnPlaySession' is triggered, the content on the view controller is changed every 20 seconds. While the timer is counting to 20, id like to indicate this with the progress bar (so the progress bar resets, each time the content changes).
class CreatedSessionViewController: UIViewController {
var createdSession: [YogaPose]!
var poseDuration: Double = 20.00
var timer = NSTimer!()
var currentPoseIndex = 1
//Outlets:
@IBOutlet var poseProgressView: UIProgressView!
@IBOutlet var lblPoseCount: UILabel!
@IBOutlet var lblPoseName: UILabel!
@IBOutlet var imgPose: UIImageView!
@IBOutlet var tvDescription: UITextView!
// Do any additional setup after loading the view:
override func viewDidLoad() {
super.viewDidLoad()
displayFirstPoseInArray()
}
func displayFirstPoseInArray(){
lblPoseCount.text = (String(currentPoseIndex) + "/" + String(createdSession.count))
lblPoseName.text = createdSession[0].title
imgPose.image = UIImage(named: String(format: "%d.jpg", (createdSession[0].id)!))
tvDescription.text = createdSession[0].desc
}
@IBAction func btnPlaySession(sender: AnyObject) {
timer = NSTimer.scheduledTimerWithTimeInterval(poseDuration, target: self, selector: "getNextPoseData", userInfo: nil, repeats: true)
}
func getNextPoseData(){
if (currentPoseIndex < createdSession.count){
setProgressBar()
lblPoseCount.text = (String(currentPoseIndex + 1) + "/" + String(createdSession.count))
lblPoseName.text = createdSession[currentPoseIndex].title
imgPose.image = UIImage(named: String(format: "%d.jpg",(createdSession[currentPoseIndex].id)!))
tvDescription.text = createdSession[currentPoseIndex].desc
currentPoseIndex += 1
print(currentPoseIndex)
}
}
func setProgressBar(){
}
}
推荐答案
好的 - 所以如果你希望进度条每秒更新一次,那么你需要一个每秒触发的计时器 - 但它会执行 20 次,并调用setProgressBar
作为选择器而不是 getNextPoseData
OK - so if you want the progress bar to update every second, then you need a timer that fires every second - but which does it 20 times, and calls setProgressBar
as selector instead of getNextPoseData
在setProgressBar
中,你需要增加一个类级别的属性,可能是indexProgressBar
,只需将进度条属性progress
设置为1.0/indexProgressBar
within setProgressBar
, you need to increment a class-level attribute, indexProgressBar
perhaps, and simply set the progress bar attribute progress
to 1.0 / indexProgressBar
如果indexProgressBar == 20
,则调用getNextPoseData
,并重置你的进度条
if indexProgressBar == 20
, then call getNextPoseData
, and reset your progress bar
这里有一个简化版,你可以这样做
and here's a simplified version of how you might do that
class ViewController: UIViewController
{
@IBOutlet weak var progressBar: UIProgressView!
var timer = NSTimer!()
var poseDuration = 20
var indexProgressBar = 0
var currentPoseIndex = 0
override func viewDidLoad()
{
super.viewDidLoad()
// initialise the display
progressBar.progress = 0.0
}
@IBAction func cmdGo(sender: AnyObject)
{
// display the first pose
getNextPoseData()
// start the timer
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "setProgressBar", userInfo: nil, repeats: true)
}
func getNextPoseData()
{
// do next pose stuff
currentPoseIndex += 1
print(currentPoseIndex)
}
func setProgressBar()
{
if indexProgressBar == poseDuration
{
getNextPoseData()
// reset the progress counter
indexProgressBar = 0
}
// update the display
// use poseDuration - 1 so that you display 20 steps of the the progress bar, from 0...19
progressBar.progress = Float(indexProgressBar) / Float(poseDuration - 1)
// increment the counter
indexProgressBar += 1
}
}
这篇关于使用 NSTimer 快速查看进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!