UIView

2009-02-20 32 views
6

के बजाय UIPickerView में स्पर्श करने का जवाब देना मेरे पास एक UIPickerView है जो उपयोग में नहीं होने पर 20% अल्फा तक फीका हो जाता है। मैं चाहता हूं कि उपयोगकर्ता पिकर को छूने में सक्षम हो और इसे वापस फीका हो।UIView

यदि मैं मुख्य दृश्य पर एक स्पर्श करता हूं तो यह काम करने के लिए मिल सकता है, लेकिन यह केवल तभी काम करता है जब उपयोगकर्ता दृश्य को छूता है। मैंने उप-वर्गीकरण UIPickerView की कोशिश की और वहां एक स्पर्श किया, लेकिन यह काम नहीं किया।

मुझे लगता है कि यह प्रतिक्रियाकर्ता श्रृंखला के साथ कुछ करने का अनुमान है, लेकिन इसे काम करने के लिए प्रतीत नहीं होता है।

उत्तर

10

मैं एक सप्ताह से अधिक समय तक इस समस्या का हल ढूंढ रहा हूं। मैं आपका जवाब दे रहा हूं भले ही आप एक साल से अधिक प्रश्न पूछ रहे हों, इससे उम्मीद है कि इससे दूसरों की मदद मिलती है।

क्षमा करें अगर मेरी भाषा बहुत तकनीकी नहीं है, लेकिन मैं उद्देश्य-सी और आईफोन विकास के लिए काफी नया हूं।

सबक्लासिंग UIpickerView इसे करने का सही तरीका है। लेकिन आपको - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event विधि को ओवरराइड करना होगा। जब भी आप स्क्रीन को स्पर्श करते हैं तो यह विधि कहा जाता है और यह उस दृश्य को वापस करता है जो स्पर्श पर प्रतिक्रिया करेगा। दूसरे शब्दों में देखें जिसका touchesBegan:withEvent: विधि कहा जाएगा।

UIPickerView में 9 सबव्यूज़ हैं! UIPickerView क्लास कार्यान्वयन में - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)eventself वापस नहीं लौटाएगा (इसका मतलब है कि touchesBegan:withEvent: जो आप सबक्लास में लिखते हैं उसे नहीं कहा जाएगा) लेकिन एक सबव्यूव वापस कर देगा, वास्तव में इंडेक्स 4 (यूआईपीकरटेबल नामक एक अनियंत्रित उपclass) पर दृश्य।

चाल - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event विधि self ताकि आप touchesBegan:withEvent:, touchesMoved:withEvent: और touchesEnded:withEvent: तरीकों पर नियंत्रण है वापस जाने के लिए बनाने के लिए है।

इन तरीकों में, UIPickerView की मानक कार्यक्षमताओं को रखने के लिए, आपको उन्हें फिर से कॉल करना याद रखना चाहिए, लेकिन UIPickerTable सबव्यू पर।

मुझे उम्मीद है कि यह समझ में आता है। मैं कोड लिख नहीं सकता, जैसे ही मैं घर पर हूं, मैं इस जवाब को संपादित करूँगा और कुछ कोड जोड़ूंगा।

8

यहाँ कुछ कोड है कि आप क्या चाहते है:

@interface TouchDetectionView : UIPickerView { 

} 
- (UIView *)getNextResponderView:(NSSet *)touches withEvent:(UIEvent *)event; 
@end 
@implementation TouchDetectionView 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UIView * hitTestView = [self getNextResponderView:touches withEvent:event]; 
    [hitTestView touchesBegan:touches withEvent:event]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UIView * hitTestView = [self getNextResponderView:touches withEvent:event]; 
    [hitTestView touchesMoved:touches withEvent:event]; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UIView * hitTestView = [self getNextResponderView:touches withEvent:event]; 
    [hitTestView touchesEnded:touches withEvent:event]; 
} 

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UIView * hitTestView = [self getNextResponderView:touches withEvent:event]; 
    [hitTestView touchesCancelled:touches withEvent:event]; 
} 

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event 
{ 
    return self; 
} 

- (UIView *)getNextResponderView:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch * touch = [touches anyObject]; 
    CGPoint point = [touch locationInView:self]; 
    UIView * hitTestView = [super hitTest:point withEvent:event]; 

    return (hitTestView == self) ? nil : hitTestView; 
} 
+0

धन्यवाद यह :) – ArunGJ

+1

काम किया इस कोड को काम करने के लिए, को छोड़कर एक स्पर्श फ्रेम कि तुरंत पिकर पहिया चारों ओर से घेरे पर होती है, तो लगता है। ऐसा करने से getNextResponderView को कॉल के अनंत लूप का कारण बनता है: withEvent: method। – pistachionut

+1

या मैंने getNextResponderView से hitTestView की बजाय शून्य को वापस करके इसे रोक दिया: साथ: अगर मैंने आसपास के पहिये को छुआ। – ArunGJ

1

ऊपर जवाब में से दोनों बहुत मददगार थे, लेकिन मैं एक UIPickerView एक UIScrollView के भीतर नेस्ट की है। मैं जीयूआई मौजूद होने पर स्क्रीन पर कहीं और लगातार प्रतिपादन कर रहा हूं। समस्या यह है कि UIPickerView पूरी तरह से अद्यतन नहीं होता है जब: एक गैर-चयनित पंक्ति टैप की जाती है, तो पिकर स्थानांतरित हो जाता है ताकि दो पंक्तियां चयन क्षेत्र में घुस जाएंगी, या एक पंक्ति खींची जाती है लेकिन उंगली UIPickerView के बाहर स्लाइड करती है। तब तक यह नहीं है जब तक UIScrollView को स्थानांतरित नहीं किया जाता है कि पिकर तुरंत अपडेट होता है। यह परिणाम बदसूरत है।

समस्या का कारण: मेरा निरंतर प्रतिपादन UIPickerView की एनीमेशन को समाप्त करने के लिए आवश्यक CPU चक्र प्राप्त करने से रोक रहा था, इसलिए सही, वर्तमान चयन दिखाने के लिए। मेरा समाधान - जो काम करता है - यह था: UIPickerView के touchesEnded:withEvent: में, थोड़ी देर के लिए मेरे प्रतिपादन को रोकने के लिए कुछ निष्पादित करें। ,

#import "SubUIPickerView.h" 

@implementation SubUIPickerView 

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event 
{ 
    [pickerTable touchesBegan:touches withEvent:event]; 
} 

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event 
{ 
    [pickerTable touchesMoved:touches withEvent:event]; 
} 

- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event 
{ 
    [singleton set_secondsPauseRendering:0.5f]; // <-- my code to pause rendering 

    [pickerTable touchesEnded:touches withEvent:event]; 
} 

- (void) touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event 
{ 
    [pickerTable touchesCancelled:touches withEvent:event]; 
} 

- (UIView*) hitTest:(CGPoint)point withEvent:(UIEvent*)event 
{ 
    if (CGRectContainsPoint(self.bounds, point)) 
    { 
     if (pickerTable == nil) 
     { 
      int nSubviews = self.subviews.count; 
      for (int i = 0; i < nSubviews; ++i) 
      { 
       UIView* view = (UIView*) [self.subviews objectAtIndex:i]; 
       if ([view isKindOfClass:NSClassFromString(@"UIPickerTable")]) 
       { 
        pickerTable = (UIPickerTable*) view; 
        break; 
       } 
      } 
     } 
     return self; // i.e., *WE* will respond to the hit and pass it to UIPickerTable, above. 
    } 
    return [super hitTest:point withEvent:event]; 
} 

@end 

और उसके बाद शीर्ष लेख SubUIPickerView.h: कोड यह

@class UIPickerTable; 

@interface SubUIPickerView : UIPickerView 
{ 
    UIPickerTable* pickerTable; 
} 

@end 

जैसा कि मैंने कहा, यह काम करता है।UIPickerView एनीमेशन को समाप्त करने की इजाजत देने के लिए एक अतिरिक्त 1/2 सेकंड के लिए रेंडरिंग रोकें (यह पहले से ही यूआईएसक्रोल व्यू को स्लाइड करता है)। NSClassFromString() का उपयोग करना मतलब है कि आप किसी भी अनियंत्रित API का उपयोग नहीं कर रहे हैं। प्रतिक्रियाकर्ता श्रृंखला के साथ मेसिंग आवश्यक नहीं था। मुझे अपने स्वयं के समाधान के साथ आने में मदद के लिए चेक्को और टायलर 230 के लिए धन्यवाद!

+0

यह आईओएस 8 के यूआईपीकर व्यू के साथ काम नहीं करता है। – MatthiasC

+0

@MatthiasC क्या आपको पता है कि यह अब और क्यों काम नहीं करता है? – stonedauwg

+0

UIPickerView के बारे में ये पोस्ट वास्तव में पुरानी हैं। यह तब वापस नया और फ्लेकी था। ऐप्पल ने शायद (उम्मीद है) अब तक पूरी तरह से इसे फिर से कार्यान्वित किया है! – electromaggot

0

सेट canCancelContentTouches और नहीं करने के लिए माता पिता को देखने के delaysContentTouches, वह मेरे लिए काम किया