Stuck: Analog Clock in Swift iOS - ios

I'm new to swift (and coding in general), and I've been working on an analog watch project. I've gotten to a place where I'm stuck. None of the println() commands are putting anything out to the console unless they arrive before the function "func setTime(){...} ". Not sure what's happening there. And I can't seem to get the hands to rotate with the math I found here. I'm trying to convert it from obj-c to swift. (I know the bottom section is very wrong at the moment) I keep messing with it (the bottom section) but I don't get any rotation.
Any help would be appreciated. I feel like I'm close.
var theTimer:NSTimer!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
println(viewDidLoad)
func setTime(){
println("set time")
self.theTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "set time", userInfo: nil, repeats: false)
let date = NSDate()
let outputFormatter = NSDateFormatter()
outputFormatter.dateFormat = "hh:mm:ss"
let newDateString:NSString = outputFormatter.stringFromDate(date)
println(newDateString)
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitSecond, fromDate: date)
var hour = components.hour
var minute = components.minute
var second = components.second
println(hour)
println(minute)
println(second)
**var secAngle = (6 * second)
var minAngle = (6 * minute)
var hourAngle = (30 * hour + minute / 2)
self.secsImage.transform = CGAffineTransformMakeRotation( CGFloat(secAngle) )
self.minsImage.transform = CGAffineTransformMakeRotation( CGFloat(Double(minAngle)) )
self.hoursImage.transform = CGAffineTransformMakeRotation( CGFloat(Double(hourAngle)) )**
println(secAngle)
println(minAngle)
println(hourAngle)
}
}

You have declared your setTime function inside your viewDidLoad function, and you have never called it. Also, your the selector for your NSTimer is not correct - it needs to be the name of the function and it needs to be a Selector, not just a string.
You should move the setTime function declaration out of viewDidLoad and I would suggest setting it up in viewWillAppear -
var theTimer:NSTimer?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
println(viewDidLoad)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.setTime()
self.theTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("setTime"), userInfo: nil, repeats: true)
}
func setTime(){
println("set time")
let date = NSDate()
let outputFormatter = NSDateFormatter()
outputFormatter.dateFormat = "hh:mm:ss"
let newDateString:NSString = outputFormatter.stringFromDate(date)
println(newDateString)
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitSecond, fromDate: date)
var hour = components.hour
var minute = components.minute
var second = components.second
println(hour)
println(minute)
println(second)
var secAngle = (6 * second)
var minAngle = (6 * minute)
var hourAngle = (30 * hour + minute / 2)
self.secsImage.transform = CGAffineTransformMakeRotation( CGFloat(secAngle) )
self.minsImage.transform = CGAffineTransformMakeRotation( CGFloat(Double(minAngle)) )
self.hoursImage.transform = CGAffineTransformMakeRotation( CGFloat(Double(hourAngle)) )**
println(secAngle)
println(minAngle)
println(hourAngle)
}
If you are new to programming, you may want to consider running through some basic coding tutorials on the web.

Related

Set high score as elapsed time in iOS

The high score in my game is time based and I am having trouble setting comparing it with the current score. This is my code so far which doesn't work:
class GameScene: SKScene, SKPhysicsContactDelegate {
// initialise value for current time
var currentTime = NSDate()
var bestTime = NSDate()
override func didMoveToView(view: SKView) {
super.didMoveToView(view)
// set the current time to 0 seconds
var date0 = NSDate();
let timeInterval = floor(date0 .timeIntervalSinceReferenceDate / 60.0) * 60.0
date0 = NSDate(timeIntervalSinceReferenceDate: timeInterval)
currentTime = date0
// call to start timer
NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "printDuration:", userInfo: NSDate(), repeats: true)
Then
func printDuration(timer: NSTimer) {
if self.view?.paused == false {
guard let userInfo = timer.userInfo else {
return
}
guard let startDate = userInfo as? NSDate else {
return
}
let duration = NSDate().timeIntervalSinceDate(startDate)
currentTime = NSDate(timeIntervalSinceReferenceDate: duration)
currentTimeValueLabel.text = "\(NSString(format:"%3.2f", duration))"
}
}
I want to be able to do something like below where I am able to compare the time in both variables and set the higher one accordingly:
if (currentTime > highScore) {
highScore = currentTime
highScoreLabel.text = "\(NSString(format:"%3.2f", highScore))"
}
You can set startDate = NSDate() in didMoveToView() at the beginning and then compare the two to see if duration is higher by typing
if duration > highScore {
highScore = duration
}

Format timer label to hours:minutes:seconds in Swift

I have an NSTimer which counts DOWN from 2 hours until 0.
Here are some of my code:
var timer = NSTimer()
let timeInterval:NSTimeInterval = 0.5
let timerEnd:NSTimeInterval = 0.0
var timeCount:NSTimeInterval = 7200.0 // seconds or 2 hours
// TimeString Function
func timeString(time:NSTimeInterval) -> String {
let minutes = Int(time) / 60
let seconds = time - Double(minutes) * 60
let secondsFraction = seconds - Double(Int(seconds))
return String(format:"%02i:%02i.%01i",minutes,Int(seconds),Int(secondsFraction * 10.0))
}
The Timer Label is:
TimerLabel.text = "Time: \(timeString(timeCount))"
HOWEVER, my timer label shows as:
Time: 200:59.0
How do I format my timer label to look like this:
Time: 01:59:59 // (which is hours:minutes:seconds)?
[Please note that I have no problems with my countdown timer, I only need to know how to CHANGE THE TIME FORMAT using the TimeString function.]
EDIT:
Someone mentioned that my question is a possible duplicate of this one: Swift - iOS - Dates and times in different format. HOWEVER, I am asking on how do I change the time format using the TimeString function that I gave above. I am not asking for another WAY on how to do it.
For instance:
let minutes = Int(time) / 60
gives me "200" minutes. etc.
Your calculations are all wrong.
let hours = Int(time) / 3600
let minutes = Int(time) / 60 % 60
let seconds = Int(time) % 60
return String(format:"%02i:%02i:%02i", hours, minutes, seconds)
#rmaddy's solution is accurate and answers the question. However, neither the question nor the solution take into account international users. I suggest using DateComponentsFormatter and let the framework handle the calculations and formatting. Doing so makes your code less error prone and more future proof.
I came across this blog post that provides a concise solution:
http://crunchybagel.com/formatting-a-duration-with-nsdatecomponentsformatter/
Pulled from that post, this is the code snippet that would replace the code you're currently using to make your calculations. Updated for Swift 3:
let duration: TimeInterval = 7200.0
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .positional // Use the appropriate positioning for the current locale
formatter.allowedUnits = [ .hour, .minute, .second ] // Units to display in the formatted string
formatter.zeroFormattingBehavior = [ .pad ] // Pad with zeroes where appropriate for the locale
let formattedDuration = formatter.string(from: duration)
Swift5
var totalSecond = Int()
var timer:Timer?
call startTimer() based on requirement-
func startTimer(){
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(countdown), userInfo: nil, repeats: true)
}
#objc func countdown() {
var hours: Int
var minutes: Int
var seconds: Int
if totalSecond == 0 {
timer?.invalidate()
}
totalSecond = totalSecond - 1
hours = totalSecond / 3600
minutes = (totalSecond % 3600) / 60
seconds = (totalSecond % 3600) % 60
timeLabel.text = String(format: "%02d:%02d:%02d", hours, minutes, seconds)
}
Done
The best way to implement a Timer in Swift (swift 4 works fine).
Declare the variable secs: Int and assign the value, in seconds, of the timer.
Then with the Timer () function, discount one second at a time and pass it to this function.
var secs = 0
var timer = Timer()
func startTimer(segs: Int) {
seg = segs
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerDiscount), userInfo: nil, repeats: true)
}
func timerDiscount() {
let hours = secs / 3600
let mins = secs / 60 % 60
let secs = secs % 60
let restTime = ((hours<10) ? "0" : "") + String(hours) + ":" + ((mins<10) ? "0" : "") + String(mins) + ":" + ((secs<10) ? "0" : "") + String(secs)
}
Declare the variables hours ,minutes and seconds and copy paste the below code it works fine.
if counter > 0 {
let hours = counter / 3600
let minutes = counter / 60
let seconds = counter % 60
counter = counter - 1
timerLbl.text = "\(hours):\(minutes):\(seconds)"
}

Adding and Subtracting times in Swift

I've written some of this in pseudo code because I don't know the syntax for it. I'd like to have the timeLeftLabel.text reflect how many hours, minutes, and seconds are left until the 6 hours are up. My biggest problem is that I don't know how to add and subtract times. Can anyone help me?
var timer = NSTimer()
func timerResults() {
let theDate = NSDate()
var endTime = theDate //+ 6 hours
let timeLeft = endTime //- theDate
timeLeftLabel.text = "\(timeLeft)"
}
#IBOutlet weak var timeLeftLabel: UILabel!
#IBAction func IBbtnUpdateTap(sender: UIButton){
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("timerResults"), userInfo: nil, repeats: true)
}
Assuming your deployment target is iOS 8.0 or later, you should use NSDateComponentsFormatter to format your string. You want something like this:
class MyViewController : UIViewController {
#IBOutlet weak var timeLeftLabel: UILabel!
var targetDate: NSDate?
var labelUpdateTimer: NSTimer?
var timeLeftFormatter: NSDateComponentsFormatter?
#IBAction func startTimerButtonWasTapped() {
targetDate = NSDate(timeIntervalSinceNow: 6 * 60 * 60)
labelUpdateTimer = NSTimer.scheduledTimerWithTimeInterval(1,
target: self, selector: "labelUpdateTimerDidFire:", userInfo: nil, repeats: true)
timeLeftFormatter = NSDateComponentsFormatter()
timeLeftFormatter?.unitsStyle = .Abbreviated // gives e.g. "1h 20m 34s"
// You might prefer .Positional, which gives e.g. "1:20:34"
timeLeftFormatter?.allowedUnits = [ .Hour, .Minute, .Second ]
labelUpdateTimerDidFire(labelUpdateTimer!)
}
#objc func labelUpdateTimerDidFire(timer: NSTimer) {
let now = NSDate()
timeLeftLabel.text = timeLeftFormatter!.stringFromDate(now,
toDate: targetDate!)
if now.compare(targetDate!) != NSComparisonResult.OrderedAscending {
print("times up!")
labelUpdateTimer?.invalidate()
}
}
}
This will add 6 hours:
let future = now.dateByAddingTimeInterval(3600*6) // 1 hour is 3600 seconds
This will find the difference:
let difference = future.timeIntervalSinceDate(now)

NSTimer to to 4 digit label update

I just made a stopwatch with a tutorial but what I would like to do is to update my 00:00 label as 1 second increasing such as 00:01, 00:02: 00:03 and to do the same for minutes. Is there anyway of doing that? Thanks in advance!
Then you have to get the date which will start the counting from which is the current date when a particular event occurs, let's say we will start the timer when the view appears, so implement viewWillAppear as follows:
var currentDate = NSDate()
override func viewWillAppear(animated: Bool) {
currentDate = NSDate()
var timer: NSTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "updateLabel", userInfo: nil, repeats: true)
timer.fire()
}
and implement the updateLabel function:
func updateLabel() {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
var elapsedSeconds: NSTimeInterval = -self.currentDate.timeIntervalSinceNow
let minutes: Int = Int(elapsedSeconds)/60
let seconds: Int = Int(elapsedSeconds) - (minutes*60)
self.timeLabel.text = String(format: "%02d:%02d", minutes, seconds)
})
}
When formatting time elapsed, NSDateComponentsFormatter is another option:
var start: CFAbsoluteTime!
override func viewDidLoad() {
super.viewDidLoad()
start = CFAbsoluteTimeGetCurrent()
NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "handleTimer:", userInfo: nil, repeats: true)
}
lazy var formatter: NSDateComponentsFormatter = {
let _formatter = NSDateComponentsFormatter()
_formatter.allowedUnits = .CalendarUnitMinute | .CalendarUnitSecond
_formatter.zeroFormattingBehavior = .Pad
return _formatter
}()
func handleTimer(timer: NSTimer) {
let elapsed = CFAbsoluteTimeGetCurrent() - start
label.text = formatter.stringFromTimeInterval(elapsed)
}
Admittedly, that will give you the time elapsed in 0:00 format, not 00:00 format.
This is Objective-C, but you'll get the idea:
-(void) updateTotalTime
{
int forHours = timeInSeconds / 3600,
remainder = timeInSeconds % 3600,
forMinutes = remainder / 60,
forSeconds = remainder % 60;
[elapsedTime setString:[NSString stringWithFormat:NSLocalizedString(#"elapsedTime", nil)
,forHours
,forMinutes
,forSeconds]];
}
and in my Localizable.strings:
"elapsedTime" = "Time: %02d:%02d:%02d";

Updating time function every second in NSDate() - Swift

I have a function that gives me a time when the app is opened but I don't want it to be static I want it to be dynamic, updating every second. I thought of it being in a loop, but I don't know how to go about doing it. Doing a loop could work but if you have a better way of doing it please answer. Here is my function:
func timeNowString() -> NSString {
let date = NSDate()
var outputFormat = NSDateFormatter()
outputFormat.locale = NSLocale(localeIdentifier:"en_US")
outputFormat.dateFormat = "HH:mm:ss"
let timeString = outputFormat.stringFromDate(date)
return timeString;
}
Question: How do I make this function dynamic? So it goes and updates every second instead of it being a static label.
If you have any questions please comment down below.
Try this. You'll need to add a label and a button and hook them up to the textLabel and start IBAction. You can modify this to add hours (just minutes/seconds here) and add a timer.invalidate() where you need it.
import UIKit
class ViewController: UIViewController {
#IBOutlet var timeLabel: UILabel!
#IBAction func start(sender: AnyObject) {
var timer = NSTimer()
if !timer.valid {
let selector : Selector = "countTime"
timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target:self, selector: selector, userInfo: nil, repeats: true)
startTime = NSDate.timeIntervalSinceReferenceDate()
}
let timeNow = timeNowString() as String
for item in timeNow {
time = timeNow.componentsSeparatedByString(":")
}
}
var time = [String]()
var startTime = NSTimeInterval()
override func viewDidLoad() {
super.viewDidLoad()
}
func countTime() {
var currentTime = NSDate.timeIntervalSinceReferenceDate()
var elapsedTime: NSTimeInterval = currentTime - startTime
var adjustedTime = Int(elapsedTime) + 3600*time[0].toInt()! + 60*time[1].toInt()! + time[0].toInt()!
var hours = Int(Double(adjustedTime)/3600.0)
let minutes = Int(Double(adjustedTime - hours*3600)/60.0)
let seconds = adjustedTime - hours*3600 - minutes*60
let startHours = hours > 9 ? String(hours):"0" + String(hours)
let startMinutes = minutes > 9 ? String(minutes):"0" + String(minutes)
let startSeconds = seconds > 9 ? String(seconds):"0" + String(seconds)
timeLabel.text = "\(startHours):\(startMinutes):\(startSeconds)"
}
func timeNowString() -> NSString {
let date = NSDate()
var outputFormat = NSDateFormatter()
outputFormat.locale = NSLocale(localeIdentifier:"en_US")
outputFormat.dateFormat = "HH:mm:ss"
let timeString = outputFormat.stringFromDate(date)
return timeString;
}
}

Resources