événements tactiles UITextView pas de tir
-
11-09-2019 - |
Question
J'ai UITextView
que je veux détecter un seul robinet pour.
On dirait que je serais bien avec dépassement simplement touchesEnded:withEvent
et la vérification [[touches anyObject] tapCount] == 1
, mais cet événement ne se déclenche pas.
Si j'Override les 4 événements comme celui-ci:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
NSLog(@"touchesBegan (tapCount:%d)", touch.tapCount);
[super touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"touches moved");
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
NSLog(@"touchesEnded (tapCount:%d)", touch.tapCount);
[super touchesEnded:touches withEvent:event];
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"touches cancelled");
}
Je reçois la sortie comme ceci:
> touchesBegan (tapCount:1)
> touchesCancelled
> touchesBegan (tapCount:1)
> touches moved
> touches moved
> touches moved
> touchesCancelled
Il semble que je ne me l'événement touchesEnded
.
Toutes les idées?
La solution 2
Mise à jour: Je fini par utiliser ici la technique: https://devforums.apple.com/message/94569#94569
Je ne suis pas sûr que ce soit un bug ou non, mais le UITextView n'a pas besoin d'utiliser les événements tactiles pour faire le menu contextuel pour copier-coller pour 3.0, qui pourrait expliquer pourquoi il avale cet événement.
assez boiteux si vous me demandez.
Mise à jour: Je blogué sur ce ici: http: / /benscheirman.com/2009/07/detecting-a-tap-on-a-uitextview
Autres conseils
Je UITextView comme sous-classé donc, qui semble fonctionner, même avec IOS 5.0.1. La clé est de passer outre touchesBegan ainsi, non seulement touchesEnded (ce qui est ce que je suis vraiment intéressé).
@implementation MyTextView
- (id)initWithFrame:(CGRect)frame {
return [super initWithFrame:frame];
}
- (void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event {
// If not dragging, send event to next responder
if (!self.dragging)
[self.nextResponder touchesBegan: touches withEvent:event];
else
[super touchesBegan: touches withEvent: event];
}
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event {
// If not dragging, send event to next responder
if (!self.dragging)
[self.nextResponder touchesEnded: touches withEvent:event];
else
[super touchesEnded: touches withEvent: event];
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if (action == @selector(paste:))
return NO;
if (action == @selector(copy:))
return NO;
if (action == @selector(cut:))
return NO;
if (action == @selector(select:))
return NO;
if (action == @selector(selectAll:))
return NO;
return [super canPerformAction:action withSender:sender];
}
- (BOOL)canBecomeFirstResponder {
return NO;
}
- (void)dealloc {
[super dealloc];
}
Vous pouvez désactiver Couper / Copier / Coller en remplaçant la méthode canPerformAction:withSender:
, vous pouvez donc revenir NON pour toutes les actions que vous ne voulez pas autoriser.
Voir documentation UIResponder ...
Espérons que cela arrêtera vos touches d'être mangé.