2016-05-24 3 views
0

私は、私は長押しボタンを設定し、 1のように+ 1 + 1 + 1 + 1スウィフトUILongpressGestureRecognizer

import UIKit 

class ViewController: UIViewController { 

var Step : Int = 0 
var timer = NSTimer() 

@IBOutlet weak var CounterLabel: UILabel! 


@IBAction func longPressButton(sender: UILongPressGestureRecognizer) { 
    print("longPressFunc") 
} 

func addNumberToLabel(){ 
    Step = Step + 1 
    updateLabel() 
} 
func updateLabel(){ 
    //Step = Step + 1 
    CounterLabel.text = String(Step) 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longPressFunc(_:))) 
    longPressRecognizer.allowableMovement = 10 
    //longPressRecognizer.minimumPressDuration = 1.0 
    self.view.addGestureRecognizer(longPressRecognizer) 



} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

func longPressFunc(sendor: UILongPressGestureRecognizer){ 

    //timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(ViewController.addNumberToLabel), userInfo: nil, repeats: true) 
    updateLabel() 
} 
} 

答えて

-1

最初の必要性抵抗をラベル付けするに番号を追加する方法を長押しボタン のためのジョブを作成していますストーリーボードに長押し楽しみ

輸入のUIKit

クラスのViewController:のUIViewController {

var step : Int = 0 

@IBOutlet weak var CounterLabel: UILabel! 

@IBAction func tapButton(sender: AnyObject) { 
    step = step + 1 
    updateLabel() 
} 

@IBAction func longPressButton(sender: UILongPressGestureRecognizer) { 
    if sender.state == .Changed { 
     step = step + 1 
     updateLabel() 
    } 
} 
@IBAction func resetButton(sender: AnyObject) { 
    step = 0 
    updateLabel() 
} 

func updateLabel() { 
    CounterLabel.text = String(step) 
} 


override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    step = 0 
    updateLabel() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

}