javaSEがiOSアプリデザイン、開発に挑む日記

本業java SEがswift、デザインの勉強したことを残しておくブログ

swift UILongPressGestureRecognizerのアクション

UILongPressGestureRecognizerの長押し認識を使う時、長押しを認識と離す時で2回アクションが起きるのを制御するメモ

 

senderのstateで判別ができる。

 

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        var tmpV = self.view.viewWithTag(1)
        let anyTap = UILongPressGestureRecognizer(target: self, action: "anyTapGesture:")
        anyTap.minimumPressDuration = 2.0
        tmpV?.addGestureRecognizer(anyTap)
    }

    func anyTapGesture(sender: UILongPressGestureRecognizer){
        if sender.state == UIGestureRecognizerState.Began {
            println("長押しアクション")
        }else if sender.state == UIGestureRecognizerState.Ended{
            println("離した時のアクション")
        }
        
    }