LoginSignup
6
6

More than 5 years have passed since last update.

カスタムUIGestureRecognizer with Swift

Last updated at Posted at 2014-12-24

SwiftでカスタムUIGestureRecognizerをつくろうとした際にハマったのでメモ

ポイント

  • Bridging-Header.h に以下を追加する
#import <UIKit/UIGestureRecognizerSubclass.h>

これ追加しないとself.state変数にアクセスしようとした際に以下のコンパイルエラーが発生する。

cannot assign to 'state' in 'self'

サンプルソース

//
//  UICustomGestureRecognizer.swift
//
//  Created by Takatomo Okitsu on 2014/12/25.
//  Copyright (c) 2014年 Takatomo Okitsu. All rights reserved.
//

import UIKit

class UICustomGestureRecognizer :UIGestureRecognizer {

    var _timeOutTimer:NSTimer?

    override func reset() {
    }

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    }

    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
    }

    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
    }

    override func touchesCancelled(touches: NSSet, withEvent event: UIEvent) {
    }

    func invalidateTimer() {
        if _timeOutTimer != nil {
            _timeOutTimer?.invalidate()
            _timeOutTimer = nil;
        }
    }

    func handleTimeOut() {
        self.invalidateTimer()
        self.state = UIGestureRecognizerState.Failed
    }
}

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6