Swift 使用 NSTimer 的进度视图
Swift Progress View with NSTimer
我有一个进度视图栏,我想用它来指示时间。这是我在 Swift 的第一个项目,我不确定如何着手。因此,我们将不胜感激任何帮助/建议...
(使用 Xcode 7.2 和 Swift 2.0)
下面是我的视图控制器。当 'btnPlaySession' 被触发时,视图控制器上的内容每 20 秒更改一次。当计时器计数到 20 时,我喜欢用进度条来指示这一点(因此进度条会重置,每次内容更改时)。
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
在setProgressBar
内,你需要增加一个class级别的属性,也许indexProgressBar
,然后简单地将进度条属性progress
设置为1.0 / indexProgressBar
如果 indexProgressBar == 20
,则调用 getNextPoseData
,并重置您的进度条
下面是您可以如何操作的简化版本
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
}
}
继续加载程序
func setProgress() {
time += 0.1
ProgressBar.setProgress(time / 3, animated: true)
if time >= 3 {
self.time = 0.01
ProgressBar.progress = 0
let color = self.downloadProgressBar.progressTintColor
self.downloadProgressBar.progressTintColor = self.downloadProgressBar.trackTintColor
self.downloadProgressBar.trackTintColor = color
}
我有一个进度视图栏,我想用它来指示时间。这是我在 Swift 的第一个项目,我不确定如何着手。因此,我们将不胜感激任何帮助/建议...
(使用 Xcode 7.2 和 Swift 2.0)
下面是我的视图控制器。当 'btnPlaySession' 被触发时,视图控制器上的内容每 20 秒更改一次。当计时器计数到 20 时,我喜欢用进度条来指示这一点(因此进度条会重置,每次内容更改时)。
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
在setProgressBar
内,你需要增加一个class级别的属性,也许indexProgressBar
,然后简单地将进度条属性progress
设置为1.0 / indexProgressBar
如果 indexProgressBar == 20
,则调用 getNextPoseData
,并重置您的进度条
下面是您可以如何操作的简化版本
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
}
}
继续加载程序
func setProgress() {
time += 0.1
ProgressBar.setProgress(time / 3, animated: true)
if time >= 3 {
self.time = 0.01
ProgressBar.progress = 0
let color = self.downloadProgressBar.progressTintColor
self.downloadProgressBar.progressTintColor = self.downloadProgressBar.trackTintColor
self.downloadProgressBar.trackTintColor = color
}