卡住:Swift iOS 中的模拟时钟
Stuck: Analog Clock in Swift iOS
我是 swift 的新手(和一般编码),我一直在从事模拟手表项目。我已经到了卡住的地方。 None 的 println() 命令将任何内容输出到控制台,除非它们在函数 "func setTime(){...} " 之前到达。不知道那里发生了什么。而且我似乎无法用我发现的数学 here 让手旋转。我正在尝试将它从 obj-c 转换为 swift。 (我知道底部部分现在是非常错误的)我一直在弄乱它(底部部分)但是我没有得到任何旋转。
如有任何帮助,我们将不胜感激。我觉得我很接近。
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)
}
}
您已经在 中 声明了 setTime
函数,但您从未调用过它。此外,您的 NSTimer
选择器不正确 - 它需要是函数的名称并且需要是 Selector
,而不仅仅是字符串。
您应该将 setTime
函数声明移出 viewDidLoad
,我建议在 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)
}
如果您是编程新手,可能需要考虑 运行 通过网络上的一些基本编码教程。
我是 swift 的新手(和一般编码),我一直在从事模拟手表项目。我已经到了卡住的地方。 None 的 println() 命令将任何内容输出到控制台,除非它们在函数 "func setTime(){...} " 之前到达。不知道那里发生了什么。而且我似乎无法用我发现的数学 here 让手旋转。我正在尝试将它从 obj-c 转换为 swift。 (我知道底部部分现在是非常错误的)我一直在弄乱它(底部部分)但是我没有得到任何旋转。
如有任何帮助,我们将不胜感激。我觉得我很接近。
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)
}
}
您已经在 中 声明了 setTime
函数,但您从未调用过它。此外,您的 NSTimer
选择器不正确 - 它需要是函数的名称并且需要是 Selector
,而不仅仅是字符串。
您应该将 setTime
函数声明移出 viewDidLoad
,我建议在 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)
}
如果您是编程新手,可能需要考虑 运行 通过网络上的一些基本编码教程。