Iphone Int值持久化
当我的touch方法在那里开始时,我想实现longpress手势。我正在使用NSTimer并在计数器变为3时记录计数器。我意识到这是一个漫长的过程。但当我松开按钮,然后再次按下mycounter时,会保留上一个值并在上一个值上递增。尽管我将计数器Equal赋值为零。请帮助,任何帮助都会被吸引Iphone Int值持久化,iphone,swift,swift2,nstimer,ios9.2,Iphone,Swift,Swift2,Nstimer,Ios9.2,当我的touch方法在那里开始时,我想实现longpress手势。我正在使用NSTimer并在计数器变为3时记录计数器。我意识到这是一个漫长的过程。但当我松开按钮,然后再次按下mycounter时,会保留上一个值并在上一个值上递增。尽管我将计数器Equal赋值为零。请帮助,任何帮助都会被吸引 var counter : Int = 0 var timer :NSTimer? override public func touchesBegan(touches: Set<UITouc
var counter : Int = 0
var timer :NSTimer?
override public func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
timer = NSTimer()
counter == 0;
timer = NSTimer.scheduledTimerWithTimeInterval(1, target:self, selector: Selector("updateCounter"), userInfo: nil, repeats: true)
}
func updateCounter() {
print(counter++)
if (counter > 3){
timer!.invalidate()
timer = nil;
}
}
override public func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
timer!.invalidate()
timer = nil;
counter == 0;
}
override public func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if (counter>=3){
print("hello")
}
}
var计数器:Int=0
变量计时器:NSTimer?
覆盖公共函数touchsbegind(touchs:Set,withEvent:UIEvent?){
timer=NSTimer()
计数器==0;
timer=NSTimer.scheduledTimerWithTimeInterval(1,目标:self,选择器:选择器(“updateCounter”),userInfo:nil,repeats:true)
}
func updateCounter(){
打印(计数器++)
如果(计数器>3){
计时器!.invalidate()
定时器=零;
}
}
覆盖公共功能触摸已移动(触摸:设置,withEvent事件:UIEvent?){
计时器!.invalidate()
定时器=零;
计数器==0;
}
覆盖公共函数touchesend(touchs:Set,withEvent:UIEvent?){
如果(计数器>=3){
打印(“你好”)
}
}
这是经典的等式=
与赋值=
运算符混淆
timer = NSTimer() // this line is actually not needed
counter = 0
...
timer?.invalidate() // better use question mark to avoid a potential crash
timer = nil
counter = 0
并删除所有分号。为什么您决定不使用长按手势识别器