您好我写了一些代码(使用swift):
import UIKit
import Foundation
func timerFireMethod(timer1: NSTimer) { }
class ViewController: UIViewController {
func timerFireMethod(timer1: NSTimer) { }
var greytitle : UIImage = UIImage(named:"AppTitle.png")
var redtitle : UIImage = UIImage(named:"AppTitleRed.png")
@IBOutlet var maintitle: UIImageView
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib
class func scheduledTimerWithTimeInterval(_ seconds: NSTimeInterval = 0.1,
target timer1m: AnyObject!,
selector timer1: Selector,
userInfo nil: AnyObject!,
repeats true: Bool) -> NSTimer!
maintitle.image = greytitle
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
如果它说class func,我会收到错误
"Class Methods may only be declared on a type"
我知道这是一个非常新手的问题,但如果有人能解释我做错了什么,我将不胜感激!另外我注意到4行作为代码被高举,但它们是代码。
答案 0 :(得分:4)
您的问题是您正在尝试在另一个方法(timerWithInterval
)中编写方法(viewDidLoad
)。您也没有以正确的方式创建计时器。我看到你声明了一个类函数来做这个,所以我将以此为例。这样做:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib
}
class func scheduledTimerWithTimeInterval(_ seconds: NSTimeInterval = 0.1,
target timer1m: AnyObject!,
selector timer1: Selector,
userInfo nil: AnyObject!,
repeats doesRepeat: Bool) -> NSTimer!{
//maintitle.image = greytitle
var timer = NSTimer.scheduledTimerWithTimeInterval(seconds, target: timer1m, selector: timer1, userInfo: nil, repeats:doesRepeat)
(NSRunLoop.currentRunLoop()).addTimer(timer, forMode:NSDefaultRunLoopMode)
}
答案 1 :(得分:1)
要创建计时器,您应该使用
var timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "someFunction", userInfo: nil, repeats: false)