2011-06-22 7 views
5

यहाँ मैं क्या करना चाहते हैं:UIBezierPaths कैसे आकर्षित करने के लिए

मैं एक UIBezierPath है और मैं कुछ विधि करने के लिए इसे पारित करने के लिए यह तैयार हो के लिए चाहते हैं। या इसे उस विधि से खींचें जिसमें इसे बनाया गया है।

मुझे यकीन है कि कैसे जो देखने में यह में तैयार किया जाना चाहिए इंगित करने के लिए नहीं कर रहा हूँ। ड्राइंग के लिए सभी तरीकों करो

- (void)drawRect:(CGRect)rect { ...} ? 

साथ शुरू करने के लिए है मैं

- (void)drawRect:(CGRect)rect withBezierPath:(UIBezierPath*) bezierPath { ... } ?? 

मैं कैसे कहते हैं कर सकते हैं यह फ़ंक्शन, या विधि, किसी अन्य विधि से?

+0

[कैसे एक कस्टम दृश्य में एक बेज़ियर पथ आकर्षित करने के लिए] See (http://stackoverflow.com/a/34659468/3681880 फोन से पहले) एक उदाहरण के साथ एक पूर्ण स्पष्टीकरण के लिए। – Suragch

उत्तर

20

drawRect: कुछ ऐसा है जो आपको setNeedsDisplay या setNeedsDisplayInRect: पर एक संदेश पर स्वचालित रूप से लागू किया जाता है। तुम कभी drawRect: सीधे कहते हैं।

हालांकि आप कह रही है कि सभी ड्राइंग संचालन drawRect: विधि के भीतर किया जाता है में सही कर रहे हैं। विशिष्ट कार्यान्वयन, होगा

- (void)drawRect:(CGRect)rect { 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    /* Do your drawing on `context` */ 
} 

आप UIBezierPath रों उपयोग कर रहे हैं के बाद से, आप बेज़ियर रास्तों कि आप आकर्षित करने के लिए और फिर setNeedsDisplay फोन जब कुछ बदल जाता है की आवश्यकता होगी की एक सरणी बनाए रखने के लिए की आवश्यकता होगी।

- (void)drawRect:(CGRect)rect {  
    for (UIBezierPath * path in bezierPaths) { 
     /* set stroke color and fill color for the path */ 
     [path fill]; 
     [path stroke]; 
    } 
} 

जहां bezierPathsUIBezierPath रों की एक सरणी है।

+0

मैं देखता हूं। कोड के किस हिस्से में मुझे UIBezierPaths की सरणी बनाने की आवश्यकता है? –

+0

इसे अपने विचार में एक संपत्ति के रूप में घोषित करें। –

+0

धन्यवाद, इससे बहुत मदद मिली, और मुझे नौकरी मिल गई (अब के लिए क्रमबद्ध)। –

3

जब आप कस्टम आपके विचार, आप उपवर्ग पर -drawRect: अधिलेखित कर सकते हैं की जरूरत है:

- (void)drawRect:(CGRect)rect 
{ 
    // config your context 
    [bezierPath stroke]; 
} 

संपादित करें: सीधे -stroke कर कोड का उपयोग कर अधिक कॉम्पैक्ट।

+1

उत्सुक - किसी भी कारण से आपने कोरग्राफिक्स विधियों का उपयोग केवल '- [UIBezierPath स्ट्रोक] 'के बजाय किया था? –

+0

@ बेन: धन्यवाद, मुझे याद आती है। – cxa

+1

यदि आप UIKit के साथ ऐसा करने जा रहे हैं तो आपको संदर्भ प्राप्त करने की भी आवश्यकता नहीं है। –

6

सबसे पहले, एक इवर

@interface SomeView { 
    UIBezierPath * bezierPath; 
} 
@property(nonatomic,retain) UIBezierPath * bezierPath; 
... 
@end 
.... 
- (void)someMethod { 
    self.bezierPath = yourBezierPath; 
    [self setNeedsDisplayInRect:rectToRedraw]; 
} 
-drawRect में

में अपने पथ को बचाने:

- (void)drawRect:(CGRect)rect { 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
    CGContextSetLineWidth(currentContext, 3.0); 
    CGContextSetLineCap(currentContext, kCGLineCapRound); 
    CGContextSetLineJoin(currentContext, kCGLineJoinRound); 
    CGContextBeginPath(currentContext); 
    CGContextAddPath(currentContext, bezierPath.CGPath); 
    CGContextDrawPath(currentContext, kCGPathStroke); 
} 
1

केवल ड्राइंग एक विधि -drawRect: कहा जाता है के अंदर होता है (जब एक दृश्य की जरूरत के रूप में चिह्नित है, जो स्वत: कहा जाता है setNeedsDisplay के माध्यम से प्रदर्शित)। तो एक drawRect:withBezierPath: विधि स्वतः शुरू हो जाती हो कभी नहीं होगा। इसे निष्पादित करने का एकमात्र तरीका यह है कि यदि आप इसे स्वयं कहते हैं।

बार जब आप एक UIBezierPath है, हालांकि, यह बहुत आसान यह आकर्षित करने के लिए है:

- (void)drawRect:(CGRect)rect { 
    UIBezierPath *path = ...; // get your bezier path, perhaps from an ivar? 
    [path stroke]; 
} 

कोर ग्राफिक्स के साथ आसपास futz करने के लिए करता है, तो सब आप क्या करना चाहते एक मार्ग बनाना है कोई ज़रूरत नहीं है।

+0

ऐसा लगता है कि NSBezierPath आईओएस में काम नहीं करता है? मैंने कोशिश की, लेकिन त्रुटियां मिलीं। –

1

आप निम्नलिखित की तरह कुछ कर सकते हैं। बस एक UIColor * setStroke परिभाषित करें; ज फ़ाइल में है और आप इस strokeColor वस्तु निर्धारित करने की आवश्यकता अपने आप [myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];

- (void)drawRect:(CGRect)rect 
    { 
     [strokeColor setStroke]; // this method will choose the color from the receiver color object (in this case this object is :strokeColor) 
     for(UIBezierPath *_path in pathArray) 
      [myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0]; 
    } 

    #pragma mark - Touch Methods 
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
     myPath=[[UIBezierPath alloc]init]; 
     myPath.lineWidth = currentSliderValue; 

     UITouch *mytouch=[[touches allObjects] objectAtIndex:0]; 
     [myPath moveToPoint:[mytouch locationInView:self]]; 
     [pathArray addObject:myPath]; 
    } 
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
     UITouch *mytouch=[[touches allObjects] objectAtIndex:0]; 
     [myPath addLineToPoint:[mytouch locationInView:self]]; 
     [self setNeedsDisplay]; 

    } 

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

  • कोई संबंधित समस्या नहीं^_^