Problem z gestem: UISwipeGestureRecognizer + UISlider
Mam problem z gestem. Zaimplementowałem UISwipeGestureRecognizer, aby uzyskać przesuń w lewo i w prawo zdarzenia i to działa dobrze. Jednak problem, z którym się borykam, polega na tym, że UISlider, który mam w tym samym widoku, nie gra ładnie. Przesuwanie suwaków jest mylone jako przesunięcie w lewo/prawo.
Ktoś już wcześniej doświadczył tego problemu, ma jakieś pomysły jak go naprawić? Wielkie dzięki.Oto kod zawarty w widoku "controller": {]}
- (void)viewDidLoad {
[super viewDidLoad];
//Setup handling of LEFT and RIGHT swipes
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
}
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"Swipe Right");
//Do stuff
}
if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"Swipe Left");
//Do stuff
}
}
2 answers
Najprostszym sposobem na poradzenie sobie z tym jest prawdopodobnie uniemożliwienie rozpoznawaniu gestów dotknięcia suwaka. Możesz to zrobić, ustawiając siebie jako delegata, a następnie implementując
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isKindOfClass:[UISlider class]]) {
// prevent recognizing touches on the slider
return NO;
}
return YES;
}
Jeśli to nie zadziała, możliwe, że suwak rzeczywiście ma podglądy, które otrzymują dotyk, więc możesz przejść do łańcucha superview
, testując każdy Widok po drodze.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-23 03:14:20
Skończyło się na tym, że to działa tuż przed tym, jak Kevin odpowiedział powyżej. Oto kod, którego użyłem, ale Kevin wygląda czysto (nie testowałem Kevina):
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
BOOL AllowSwipes = YES;
CGPoint point1 = [touch locationInView:_sliderLeft];
CGPoint point2 = [touch locationInView:_sliderRight];
//Left slider test
if ([_sliderLeft hitTest:point1 withEvent:nil] != nil) {
AllowSwipes = NO;
NSLog(@"On Left Slider");
}
//Right slider test
if ([_sliderRight hitTest:point2 withEvent:nil] != nil) {
AllowSwipes = NO;
NSLog(@"On Right Slider");
}
}
return AllowSwipes;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-22 15:44:19