gpt4 book ai didi

ios - 卡住 : Analog Clock in Swift iOS

转载 作者:行者123 更新时间:2023-11-28 05:29:21 27 4
gpt4 key购买 nike

我是 swift 的新手(一般来说是编码新手),我一直在从事模拟手表项目。我已经到了卡住的地方。除非 println() 命令在函数“func setTime(){...}”之前到达,否则没有一个 println() 命令会向控制台输出任何内容。不知道那里发生了什么。而且我似乎无法用我发现的数学来旋转手 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)
}
}

最佳答案

您已经在 viewDidLoad 函数声明了您的 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)
}

如果您是编程新手,您可能需要考虑浏览网络上的一些基本编码教程。

关于ios - 卡住 : Analog Clock in Swift iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29525405/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com