UITouch
ジェスチャに関連付けられたオブジェクトを取得する方法はありますか? UIGestureRecognizer
これにはメソッドがないようです。
6 に答える
Jay の言うとおりです。サブクラスが必要です。サイズについては、これを試してみてください。これは私のプロジェクトの 1 つです。DragGestureRecognizer.h 内:
@interface DragGestureRecognizer : UILongPressGestureRecognizer {
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
@end
@protocol DragGestureRecognizerDelegate <UIGestureRecognizerDelegate>
- (void) gestureRecognizer:(UIGestureRecognizer *)gr movedWithTouches:(NSSet*)touches andEvent:(UIEvent *)event;
@end
そして DragGestureRecognizer.m では:
#import "DragGestureRecognizer.h"
@implementation DragGestureRecognizer
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
if ([self.delegate respondsToSelector:@selector(gestureRecognizer:movedWithTouches:andEvent:)]) {
[(id)self.delegate gestureRecognizer:self movedWithTouches:touches andEvent:event];
}
}
@end
もちろん、実装する必要があります
- (void) gestureRecognizer:(UIGestureRecognizer *)gr movedWithTouches:(NSSet*)touches andEvent:(UIEvent *)event;
デリゲートのメソッド -- 例:
DragGestureRecognizer * gr = [[DragGestureRecognizer alloc] initWithTarget:self action:@selector(pressed:)];
gr.minimumPressDuration = 0.15;
gr.delegate = self;
[self.view addGestureRecognizer:gr];
[gr release];
- (void) gestureRecognizer:(UIGestureRecognizer *)gr movedWithTouches:(NSSet*)touches andEvent:(UIEvent *)event{
UITouch * touch = [touches anyObject];
self.mTouchPoint = [touch locationInView:self.view];
self.mFingerCount = [touches count];
}
興味のある場所だけであれば、サブクラス化する必要はなく、UIGestureRecognizer からタップの場所の変更が通知されます。
ターゲットで初期化:
self.longPressGestureRecognizer = [[[UILongPressGestureRecognizer alloc] initWithTarget: self action: @selector(handleGesture:)] autorelease];
[self.tableView addGestureRecognizer: longPressGestureRecognizer];
UIGestureRecognizerStateChanged を処理して、場所の変更を取得します。
- (void)handleGesture: (UIGestureRecognizer *)theGestureRecognizer {
switch (theGestureRecognizer.state) {
case UIGestureRecognizerStateBegan:
case UIGestureRecognizerStateChanged:
{
CGPoint location = [theGestureRecognizer locationInView: self.tableView];
[self infoForLocation: location];
break;
}
case UIGestureRecognizerStateEnded:
{
NSLog(@"Ended");
break;
}
default:
break;
}
}
ジェスチャの場所のみを確認する必要がある場合は、UIGestureRecognizer オブジェクトで locationInView: または locationOfTouch:inView: を呼び出すことができます。ただし、他のことをしたい場合は、サブクラス化する必要があります。
独自の UIGestureRecognizer を作成している場合は、タッチ オブジェクトをオーバーライドできます。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
または同等のものが移動、終了、またはキャンセルされました
Apple docsには、サブクラス化に関する多くの情報があります
これは、任意の UIView に長押しを追加するメソッドです。
これにより、任意の数の UIView で longpress を実行できます。この例では、タグを使用して UIView メソッドへのアクセスを制限していますが、isKindOfClass も使用できます。
(LongPress がアクティブになる前に touchesBegan が発生するため、LongPress には touchesMoved を使用する必要があることがわかりました)
サブクラス - .h
#import <UIKit/UIKit.h>
#import <UIKit/UIGestureRecognizerSubclass.h>
@interface MyLongPressGestureRecognizer : UILongPressGestureRecognizer
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
@property (nonatomic) CGPoint touchPoint;
@property (strong, nonatomic) UIView* touchView;
@end
サブクラス - .m
#import "MyLongPress.h"
@implementation MyLongPressGestureRecognizer
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
{
UITouch * touch = [touches anyObject];
self.touchPoint = [touch locationInView:self.view];
self.touchView = [self.view hitTest:[touch locationInView:self.view] withEvent:event];
}
#pragma mark - Setters
-(void) setTouchPoint:(CGPoint)touchPoint
{
_touchPoint =touchPoint;
}
-(void) setTouchView:(UIView*)touchView
{
_touchView=touchView;
}
@end
ビューコントローラー - .h
//nothing special here
ビューコントローラー - .m
#import "ViewController.h"
//LOAD
#import "MyLongPress.h"
@interface ViewController ()
//lOAD
@property (strong, nonatomic) MyLongPressGestureRecognizer* longPressGesture;
@end
@implementation PDViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//LOAD
self.longPressGesture =[[MyLongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];
self.longPressGesture.minimumPressDuration = 1.2;
[[self view] addGestureRecognizer:self.longPressGesture];
}
//LOAD
-(void) setLongPressGesture:(MyLongPressGestureRecognizer *)longPressGesture {
_longPressGesture = longPressGesture;
}
- (void)longPressHandler:(MyLongPressGestureRecognizer *)recognizer {
if (self.longPressGesture.touchView.tag >= 100) /* arbitrary method for limiting tap */
{
//code goes here
}
}
シンプルで高速:
NSArray *touches = [recognizer valueForKey:@"touches"];
レコグナイザーはどこにありますかUIGestureRecognizer