2010-05-24 13 views

Odpowiedz

7

Możesz po prostu dodać UITapGestureRecognizer do suwaka, a następnie wyciągnąć UIEvent i dotknięcia związane z nim, aby dowiedzieć się, gdzie wzdłuż UISlider kran miał miejsce. Następnie ustaw wartość suwaka na tę wyliczoną wartość.

UPDATE:

pierwsze, konfiguracja Twój suwak i dodać rozpoznawania gestów do niego.

UISlider *slider = [[[UISlider alloc] init] autorelease]; 
… 
<slider setup> 
… 
UITapGestureRecognizer *gr = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sliderTapped:)] autorelease]; 
[slider addGestureRecognizer:gr]; 

Następnie realizacji selektor

- (void)sliderTapped:(UIGestureRecognizer *)gestureRecognizer { 
    <left as a user excercise*> 
} 

* Podpowiedź: Read the docs aby dowiedzieć się, jak uzyskać locationInView ekstrapolować i dowiedzieć się, co suwak powinien być

+0

można z grubsza obraz tego, jak wprowadzić UITapGestureRecognizer na suwak? – suse

19

Oto część „lewo ćwiczenie użytkownika ":

- (void) tapped: (UITapGestureRecognizer*) g { 
    UISlider* s = (UISlider*)g.view; 
    if (s.highlighted) 
     return; // tap on thumb, let slider deal with it 
    CGPoint pt = [g locationInView: s]; 
    CGFloat percentage = pt.x/s.bounds.size.width; 
    CGFloat delta = percentage * (s.maximumValue - s.minimumValue); 
    CGFloat value = s.minimumValue + delta; 
    [s setValue:value animated:YES]; 
} 
11

Sposób w jaki zrobiłem to podklasy suwak i sprawdź w touchesBegan. Jeśli użytkownik kliknie obszar przycisku kciukiem (co śledzenie), a następnie ignorować kran, ale gdziekolwiek indziej na potencjometr robimy: :

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint touchLocation = [touch locationInView:self]; 

    // if we didn't tap on the thumb button then we set the value based on tap location 
    if (!CGRectContainsPoint(lastKnownThumbRect, touchLocation)) { 

     self.value = self.minimumValue + (self.maximumValue - self.minimumValue) * (touchLocation.x/self.frame.size.width); 
    } 

    [super touchesBegan:touches withEvent:event]; 
} 

- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value { 

    CGRect thumbRect = [super thumbRectForBounds:bounds trackRect:rect value:value]; 
    lastKnownThumbRect = thumbRect; 
    return thumbRect; 
} 
+1

najlepsze najlepsze rozwiązanie –

+1

jeśli użyłeś niestandardowego "setThumbImage", użyj 'self.value = self.minimumValue + (self.maximumValue - self.minimumValue) * ((touchLocation.x - self.currentThumbImage.size.width/2)/(self.frame.size.width-self.currentThumbImage.size.width)); 'in -touchesBegan. – SwiftArchitect

1

używałem podklasy kod UISlider słuchać zdarzeń valueChanged w ios6> w celu zaimplementowania funkcji typu "przyciągnij do siatki".

Ten błąd został uszkodzony podczas aktualizacji do iOS7, ponieważ nie uruchamiał już UIControlEventsValueChanged. Dla każdego, mającego ten sam problem jest ona ustalana przez dodanie odpowiedniego wiersza w if() jak poniżej:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint touchLocation = [touch locationInView:self]; 

    // if we didn't tap on the thumb button then we set the value based on tap location 
    if (!CGRectContainsPoint(lastKnownThumbRect, touchLocation)) { 

     self.value = self.minimumValue + (self.maximumValue - self.minimumValue) * (touchLocation.x/self.frame.size.width); 
     [self sendActionsForControlEvents:UIControlEventValueChanged]; 
    } 

    [super touchesBegan:touches withEvent:event]; 
} 

- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value { 

    CGRect thumbRect = [super thumbRectForBounds:bounds trackRect:rect value:value]; 
    lastKnownThumbRect = thumbRect; 
    return thumbRect; 
} 

Mam nadzieję, że ktoś pomaga :)

0

Użyłem tego kodu. Uzyskaj od: http://imagineric.ericd.net/2012/11/15/uislider-touch-to-set-value/

UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sliderTapped:)]; 
[slider addGestureRecognizer:gr]; 



- (void)sliderTapped:(UIGestureRecognizer *)g { 
     UISlider* s = (UISlider*)g.view; 
     if (s.highlighted) 
      return; // tap on thumb, let slider deal with it 
     CGPoint pt = [g locationInView: s]; 
     CGFloat percentage = pt.x/s.bounds.size.width; 
     CGFloat delta = percentage * (s.maximumValue - s.minimumValue); 
     CGFloat value = s.minimumValue + delta; 
     [s setValue:value animated:YES]; 
    } 

Mam nadzieję, że to ci pomoże.

0

Wygląda na to, że właśnie podklasy UISlider i powrót zawsze prawdziwe do beginTracking dają pożądany efekt.

iOS 10 i Swift 3

class CustomSlider: UISlider { 
    override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool { 
     return true 
    } 
} 
Powiązane problemy