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
}
}