2010-05-24 7 views
7

स्लाइडर को क्लिक की गई स्थिति पर कैसे सेट करें और आईफ़ोन प्रोग्रामिंग में यूआईस्लाइडर पर क्लिक किए गए स्थान पर स्लाइडर मान प्राप्त करें। मुझे पता है कि हम स्लाइडर को उस स्थिति में खींच सकते हैं लेकिन मैं इसे नहीं करना चाहता हूं। क्या आप कृपया मुझे बता सकते हैं कि स्लाइडर को क्लिक की गई स्थिति पर कैसे सेट करें? क्या ऐसा करना संभव है?आईफोन: क्लिकिंग स्थान पर स्थिति के लिए प्रोग्रामिंग यूआईस्लाइडर

उत्तर

7

आप स्लाइडर में बस UITapGestureRecognizer जोड़ सकते हैं, फिर यूआईआईएलआईडर और टच को इससे बाहर निकालने के लिए बाहर निकालें ताकि यह पता लगाया जा सके कि यूआईस्लाइडर टैप के साथ कहां था। फिर अपने स्लाइडर के मान को इस गणना मूल्य पर सेट करें।

अद्यतन:

पहले, सेटअप अपने स्लाइडर और इसे करने के लिए इशारा पहचानकर्ता जोड़ें।

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

तो चयनकर्ता लागू

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

* सुझाव: Read the docs locationInView वाग्विस्तार हो और यह पता लगाने की क्या स्लाइडर होना चाहिए

+0

आप कैसे स्लाइडर में UITapGestureRecognizer पेश करने का एक मोटा चित्र दे सकते हैं? – suse

19

यहाँ भाग "के रूप में छोड़ दिया है करने के लिए कैसे पता लगाने के लिए एक उपयोगकर्ता व्यायाम ":

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

जिस तरह से मैंने इसे उपclass करना है स्लाइडर और touchesBegan में चेक करें। उपयोगकर्ता अंगूठे बटन क्षेत्र पर टैप करता है (जो हम पर नज़र रखने) तो नल पर ध्यान न दें, लेकिन किसी भी और कहाँ trackbar पर हम करते हैं: :

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

अब तक का सबसे अच्छा समाधान –

+1

यदि आपने कस्टम -setThumbImage का उपयोग किया है, तो 'self.value = self.minimumValue + (self.maximumValue - self.minimumValue) * ((touchLocation.x - self.currentThumbImage.size.width/2)/(self.frame.size.width-self.currentThumbImage.size.width)); 'इन-टच बेगन। – SwiftArchitect

1

मैं subclassed UISlider कोड उपयोग कर रहा था ValueChanged घटनाओं को सुनने के लिए ios6> स्नैप-टू-ग्रिड प्रकार के फ़ंक्शन को कार्यान्वित करने के लिए।

आईओएस 7 में अपग्रेड करते समय यह टूट गया था क्योंकि यह अब UIControlEventsValueChanged को निकाल दिया गया था। एक ही समस्या यह तय हो गई है होने अगर() के रूप में नीचे में एक पंक्ति जोड़कर किसी के लिए:

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

आशा है कि यह मदद करता है किसी को :)

0

मैं इस कोड का इस्तेमाल किया है। इससे प्राप्त करें: 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]; 
    } 

आशा है कि यह आपकी मदद कर सके।

0

ऐसा लगता है कि यूआईस्लाइडर को उप-वर्गीकरण करना और शुरुआत के लिए हमेशा सत्य लौटना, वांछित प्रभाव उत्पन्न करना।

आईओएस 10 और स्विफ्ट 3

class CustomSlider: UISlider { 
    override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool { 
     return true 
    } 
} 
संबंधित मुद्दे