2012-03-30 6 views
7

मैं कतार में अपने ऐप के कुछ निष्पादन करने के लिए ग्रांड सेंट्रल डिस्पैच विधियों का उपयोग करता हूं। मैं उस कतार पर गणना में बटन के लिए फ्रेम तय करता हूं। मैं चाहता हूं कि मेरा ऐप घूमने के बाद अपने स्क्रेन को फिर से खींचें और नए फ्रेम की गणना करे।कोड के ब्लॉक का नाम कैसे दें और इसे एक अलग विधि में कॉल करें?

CGFloat a=123, b=24; 
    dispatch_async(drawingQue, ^{ 
     //needed loops to get the total button count-how many ones will be drawn et.. 
     for(int x=0;x<someCount<x++){ 
      for(int y=0;y<anotherCount;y++){ 

     //needed frame&name ect assingments 

     button.frame= CGRectMake(x+y, x-y, a, b); 
     [button setTitle:@"abc"];}} 
     }; 

यहाँ मैं क्या चाहते है, मैं कैसे इस ब्लॉक को एक नाम दे सकते हैं और यह

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
} 

प्रतिनिधि विधि में फिर से उपयोग: यहाँ मैं क्या कर से कुछ छद्म कोड व्याख्या है? उदाहरण के लिए, यदि रोटेशन परिदृश्य है, तो मैं 123 के बजाय a=234 का उपयोग करना चाहता हूं .. कृपया कोई मदद करें। अग्रिम धन्यवाद ..

उत्तर

6

ब्लॉक प्रकार का एक उदाहरण चर घोषित करें और Block_copy का उपयोग ब्लॉक रखने के लिए:

@interface My { 
    void (^myBlock)(void); 
} 
@end 

myBlock = Block_copy(^{ 
    ...block code... 
}); 

// later call it 
myBlock(); 

// don't forget to release it in dealloc 

यह शाब्दिक के दायरे से बाहर यह भंडारण से पहले प्रतिलिपि ब्लॉक करने के लिए महत्वपूर्ण है (^{...}), क्योंकि मूल ब्लॉक ढेर पर संग्रहीत है और जब दायरा निकलता है तो मर जाएगा।

+0

यह ध्यान रखना महत्वपूर्ण है कि एक ivar में आयोजित ब्लॉक के अंदर 'स्वयं' या किसी भी ivar का जिक्र करना एक बनाए रखने चक्र और इस प्रकार एक रिसाव बनाएगा। आप ivblock आईडी ब्लॉक स्वयं = स्वयं; 'सूचक के माध्यम से या ब्लॉक _before_ 'dealloc' को जारी करने की व्यवस्था करके ivars का संदर्भ देकर चक्र तोड़ सकते हैं। –

1

बस एक @property एक ब्लॉक है कि, यह स्टोर बनाने के लिए, और बाद में पुनः उपयोग:

typedef void (^MyBlock)(CGFloat, CGFloat); 
... 
@property(readwrite, copy) MyBlock buttonFramesBlock; 
... 
@synthesize buttonFramesBlock; 
... 
self.buttonFramesBlock = ^(CGFloat a, CGFloat b){ 
    //needed loops to get the total button count-how many ones will be drawn et.. 
    for(int x=0;x<someCount<x++){ 
     for(int y=0;y<anotherCount;y++){ 

    //needed frame&name ect assingments 

    button.frame= CGRectMake(x+y, x-y, a, b); 
    [button setTitle:@"abc"];}} 
}; 
... 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    dispatch_async(drawingQue, ^{ 
     self.buttonFramesBlock(234,someOtherInt); 
    }); 
} 
1

पहले, कभी नहीं मुख्य थ्रेड के बाहर यूआई बदल जाते हैं। तो अपने की तरह कुछ में अपने कोड को संशोधित करना चाहिए:

dispatch_async(drawingQue, ^{ 
    // ... 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     button.frame= CGRectMake(x+y, x-y, a, b); 
     [button setTitle:@"abc"]; 
    }); 
}); 

दूसरा, विधि shouldAutorotateToInterfaceOrientation अंदर यूआई कभी नहीं। उस विधि के अंदर आपको बस इतना करना है कि दृश्य घुमाएगा या नहीं। उदाहरण के लिए, कुछ मामलों में जहां आपके पास व्यू कंट्रोलर पदानुक्रम है, तो दृश्य YESshouldAutorotateToInterfaceOrientation में वापस लौटने पर भी दृश्य घुमाया नहीं जा सकता है।

तो, आप विधि के अंदर अपना कोड बुलाना चाहिए:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 

यह कई तरीकों से प्राप्त किया जा सकता है। सरल (और एक मेरा सुझाव है) एक मानक ऑब्जेक्टिव-सी विधि का उपयोग करने के लिए है:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) { // landscape 
     [self rotateButtonWithA:234 b:24]; 
    } else { // portrait 
     [self rotateButtonWithA:123 b:24]; 
    } 

} 

- (void)rotateButtonWithA:(CGFloat)a b:(CGFloat)b 
{ 
    dispatch_async(drawingQue, ^{ 
     // ... 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      button.frame= CGRectMake(x+y, x-y, a, b); 
      [button setTitle:@"abc"]; 
     }); 
    }); 
} 

आप ऐसा नहीं वास्तव में कई स्थानों से ब्लॉक में ही कॉल करने के लिए की जरूरत है। लेकिन अगर आप अभी भी ऐसा करना चाहते हैं, तो यहां कई जवाब हैं जो आपको दिखाते हैं कि यह कैसे करें।

+0

आपका उत्तर उचित दिखता है लेकिन मैं इस स्थिति को कैसे संभाल सकता हूं? यदि मैं फ्रेम को स्थिर रूप से तय करता हूं, जब यह घूमता है तो यह भयानक लगता है – ilhnctn

+0

अग्रिम धन्यवाद। वास्तव में अच्छा जवाब-स्पष्टीकरण। – ilhnctn

संबंधित मुद्दे