C'è un modo per ottenere gli oggetti UITouch
associati a un gesto? UIGestureRecognizer
non sembra avere alcun metodo per questo.Come ottenere gli oggetti UITouch per un UIGestureRecognizer
risposta
Jay ha ragione ... vorrai una sottoclasse. Prova questo per dimensioni, è da uno dei miei progetti. In 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
E in 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
Naturalmente, sarà necessario implementare il metodo
- (void) gestureRecognizer:(UIGestureRecognizer *)gr movedWithTouches:(NSSet*)touches andEvent:(UIEvent *)event;
nel vostro delegato - per esempio :
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];
}
Se si sta scrivendo il proprio UIGestureRecognizer è possibile ottenere gli oggetti di contatto sovrascrivendo:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
o l'equivalente spostato, si è conclusa o annullata
Il Apple docs ha un sacco di informazioni sulla sottoclasse
Se hai solo bisogno di trovare la posizione del gesto, puoi chiamare localInView: o locationOfTouch: inView: sull'oggetto UIGestureRecognizer. Tuttavia, se vuoi fare qualcos'altro, allora dovrai sottoclassi.
Se è solo il luogo a cui sei interessato, non devi sottoclasse, ti verrà notificato il cambio di posizione del rubinetto da UIGestureRecognizer.
inizializzazione con l'obiettivo:
self.longPressGestureRecognizer = [[[UILongPressGestureRecognizer alloc] initWithTarget: self action: @selector(handleGesture:)] autorelease];
[self.tableView addGestureRecognizer: longPressGestureRecognizer];
Maniglia UIGestureRecognizerStateChanged per ottenere la posizione modifiche:
- (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;
}
}
Questo doesn' t ottenere 'UITouch', ottiene solo un' CGPoint' per la cornice locale della vista specifica. – Chris
Ecco un metodo per ottenere una pressione prolungata aggiunto ad un UIView arbitraria.
Ciò consente di eseguire longpress su qualsiasi numero di UIViews. In questo esempio, limito l'accesso al metodo UIView tramite tag, ma è possibile utilizzare anche isKindOfClass.
(Da quello che ho trovato è necessario utilizzare touchesMoved per longpress perché incendi touchesBegan prima della longpress diventa attivo)
sottoclasse - .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
sottoclasse - .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
viewcontroller - .h
//nothing special here
viewcontroller -.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
}
}
semplice e veloce:
NSArray *touches = [recognizer valueForKey:@"touches"];
Dove riconoscitore è il vostro UIGestureRecognizer
iOS 8 [
Non dimenticate aggiungere #import per depositare DragGestureRecognizer.h –
HotJard