2010-05-24 9 views
7

Come impostare il cursore sulla posizione cliccata e ottenere il valore del cursore sulla posizione cliccata su UISlider nella programmazione iPhone. so che possiamo trascinare il cursore in quella posizione ma non voglio farlo. Puoi cortesemente dirmi come impostare il cursore sulla posizione cliccata? È possibile farlo?iPhone: programmazione di UISlider per posizionarsi nella posizione cliccata

risposta

7

È possibile semplicemente aggiungere un UITapGestureRecognizer al cursore, quindi estrarre l'UIEvent ei Tocchi associati ad esso per capire dove si è verificato il tocco lungo l'UISlider. Quindi imposta il valore del tuo cursore su questo valore calcolato.

UPDATE:

In primo luogo, impostare il dispositivo di scorrimento e aggiungere il sistema di riconoscimento gesto ad esso.

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

quindi implementare il selettore

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

* Suggerimento: Read the docs di capire come ottenere il locationInView estrapolato e capire che cosa il cursore dovrebbe essere

+0

si può dare un'idea approssimativa di come introdurre UITapGestureRecognizer in slitta? – suse

19

qui è la parte "sinistra come un esercizio utente ":

- (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

Il modo in cui l'ho fatto è quello di sottoclassi cursore e check-in touchesBegan. Se l'utente tocca l'area del pulsante del pollice (che traccia) poi ignorare il rubinetto, ma qualsiasi altra parte sulla trackbar facciamo: :

-(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

soluzione migliore –

+1

se si è utilizzata un'impostazione personalizzata -setThumb, utilizzare '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

stavo usando il codice UISlider sottoclasse di ascoltare gli eventi ValueChanged in ios6> per implementare un tipo di funzione snap-to-grid.

Si è verificato un errore durante l'aggiornamento a iOS7 poiché non ha più attivato UIControlEventsValueChanged. Per chiunque abbia lo stesso problema si è risolto con l'aggiunta di una linea nella if() come di seguito:

-(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; 
} 

Speranza che aiuta qualcuno :)

0

ho usato questo codice. Get from: 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]; 
    } 

Spero che questo possa aiutarti.

0

Sembra appena la sottoclasse di UISlider e il ritorno sempre vero al beginTracking produce l'effetto desiderato.

iOS 10 e Swift 3

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