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 [
- 1. Come ottenere la posizione UITouch da UIGestureRecognizer
- 2. Ascolto dell'evento UITouch insieme a UIGestureRecognizer
- 3. Differenziazione tra oggetti UITouch su iPhone
- 4. UIGestureRecognizer per parte di un UIView
- 5. Come annullare/ripristinare un UIGestureRecognizer
- 6. Come concatenare due UIGestureRecognizer insieme?
- 7. UIGestureRecognizer per trascinare UIView
- 8. Ottenere gli ID di tutti gli oggetti in un elenco
- 9. Come aggiungere un seguito a un UIGestureRecognizer
- 10. Come "==" funziona per gli oggetti?
- 11. array_unique per gli oggetti?
- 12. UIGestureRecognizer funziona su UIWebView?
- 13. Come ottenere l'elenco di tutti gli oggetti? - PostgreSQL
- 14. Come cancellare una sequenza di eventi UITouch?
- 15. come ottenere un elenco di tutte le proprietà estese per tutti gli oggetti
- 16. Algoritmo per raggruppare gli oggetti
- 17. UIGestureRecognizer Detection Order
- 18. UIGestureRecognizer ed emissione UITableViewCell
- 19. Come ispezionare gli oggetti om?
- 20. Invariant Violazione: Gli oggetti non sono validi come React bambino (per gli oggetti nidificati)
- 21. come passare argomento per l'azione in azione UIGestureRecognizer initWithTarget
- 22. Come compareresti gli oggetti jQuery?
- 23. Come evidenziare gli oggetti ListView
- 24. jFreeChart: come nascondere gli oggetti dalla legenda?
- 25. Twig instanceof per gli oggetti ereditari
- 26. Come posso ottenere tutti gli oggetti in un modello Django che hanno un valore specifico per un campo ForeignKey?
- 27. Un modo semplice per raggruppare gli oggetti in bucket
- 28. Come contare gli oggetti JSON
- 29. Come impostare colori alternativi per gli oggetti ListView in QML
- 30. Come salvare gli oggetti caricati in un ripetitore?
Non dimenticate aggiungere #import per depositare DragGestureRecognizer.h –
HotJard