2010-07-28 14 views
8

मेरी तरीकों में से एक में पर UIBezierPath दिखा मैं इस कोड है:दृश्य

-(void)myMethod { 
    UIBezierPath *circle = [UIBezierPath 
         bezierPathWithOvalInRect:CGRectMake(75, 100, 200, 200)]; 
} 

मैं कैसे यह दृश्य पर दिखाने के लिए मिलता है?

मैंने addSubview की कोशिश की लेकिन मुझे मुझे incompatible type error दिया क्योंकि इसकी एक UIView की अपेक्षा है।

मुझे यकीन है कि यह सरल होना चाहिए।

धन्यवाद

उत्तर

18

आप कस्टम दृश्य के drawInRect: कार्यान्वयन में उदाहरण के लिए या तो fill या stroke तरीकों का उपयोग कर यह आकर्षित कर सकते हैं:

- (void)drawRect:(CGRect)rect { 
    // Drawing code 
    UIBezierPath *circle = [UIBezierPath 
        bezierPathWithOvalInRect:CGRectMake(75, 100, 200, 200)]; 
    [circle fill]; 
} 
+0

मैं इस कोशिश की, लेकिन कुछ नहीं होता। मैं अपने कस्टम व्यू में ड्राइंग कैसे प्राप्त करूं? – joec

+1

UIView सबक्लास बनाएं और वहां drawRect विधि लागू करें, यह मेरे लिए कम से कम – Vladimir

+1

काम करता है यह पुराना है, लेकिन अगर यह किसी और की मदद करता है: मैं यह निर्दिष्ट करना भूल गया कि इंटरफ़ेस बिल्डर में मेरा दृश्य "MyView" (क्लास पहचान) – Mirkules

2

आहरण विचारों के अनन्य प्रावधान नहीं है। एक कस्टम व्यू बनाएं, इसे अपना रास्ता दें, और पथ को भरने और/या स्ट्रोक करने के लिए दृश्य की drawRect: विधि लागू करें।

+0

है इसलिए मैंने एक कस्टम व्यू बनाया, फिर यह मेरे अन्य व्यू कंट्रोलर 'जोगशटल * जेएस = [[जोगशटल एटोक] initWithFrame में किया: CGRectMake (...)]; [self.view addSubView: जेएस]; 'लेकिन दृश्य पर कुछ भी खींचा नहीं गया है? – joec

+0

क्या आप कुछ और कोड पोस्ट कर सकते हैं? आप अपने जेएस व्यू पर क्या फ्रेम सेट करते हैं? जांचें कि क्या आपका बेजियर पथ दृश्य की सीमाओं के अंदर है? – Vladimir

+0

इसे हल किया - धन्यवाद। – joec

27

बस सोचा कि मैं यह जोड़ूंगा कि आपको इसे UIView की "drawRect:" विधि में आवश्यक रूप से आकर्षित करने की आवश्यकता नहीं है। आप इसे कहीं भी आकर्षित कर सकते हैं, जिसे आप प्रदान करना चाहते हैं, आप इसे यूआईजीआरएफ़िक्स छवि संदर्भ के अंदर करते हैं। मैं यह हर समय करता हूं जब मैं UIView का उप-वर्ग बनाना नहीं चाहता हूं। यहां एक कामकाजी उदाहरण दिया गया है:

UIBezierPath *circle = [UIBezierPath 
         bezierPathWithOvalInRect:CGRectMake(75, 100, 200, 200)]; 

//you have to account for the x and y values of your UIBezierPath rect 
//add the x to the width (75 + 200) 
//add the y to the height (100 + 200) 
UIGraphicsBeginImageContext(CGSizeMake(275, 300)); 

//this gets the graphic context 
CGContextRef context = UIGraphicsGetCurrentContext(); 

//you can stroke and/or fill 
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
CGContextSetFillColorWithColor(context, [UIColor lightGrayColor].CGColor); 
[circle fill]; 
[circle stroke]; 

//now get the image from the context 
UIImage *bezierImage = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 

UIImageView *bezierImageView = [[UIImageView alloc]initWithImage:bezierImage]; 

अब केवल UIImageView को एक सबव्यूव के रूप में जोड़ें।

इसके अलावा, आप इसे अन्य ड्राइंग के लिए भी उपयोग कर सकते हैं। फिर, थोड़ा सा सेटअप के बाद, यह drawRect: विधि की तरह काम करता है।

//this is an arbitrary size for example 
CGSize aSize = CGSizeMake(50.f, 50.f); 

//this can take any CGSize 
//it works like the frame.size would in the drawRect: method 
//in the way that it represents the context's size 
UIGraphicsBeginImageContext(aSize); 

//this gets the graphic context 
CGContextRef context = UIGraphicsGetCurrentContext(); 

//you can do drawing just like you would in the drawRect: method 
//I am drawing a square just for an example to show you that you can do any sort of drawing in here 
CGContextMoveToPoint(context, 0.f, 0.f); 
CGContextAddLineToPoint(context, aSize.width, 0.f); 
CGContextAddLineToPoint(context, aSize.width, aSize.height); 
CGContextAddLineToPoint(context, 0.f, aSize.height); 
CGContextClosePath(context); 

//you can stroke and/or fill 
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
CGContextSetFillColorWithColor(context, [UIColor lightGrayColor].CGColor); 
CGContextDrawPath(context, kCGPathFillStroke); 

//now get the image from the context 
UIImage *squareImage = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 

UIImageView *squareImageView = [[UIImageView alloc]initWithImage:squareImage]; 

संपादित करें: एक बात मैं जोड़ना चाहिए कि इस तरह के किसी भी आधुनिक दिन ड्राइंग के लिए, आप

UIGraphicsBeginImageContext(size); 

बाहर गमागमन किया जाना चाहिए के लिए

UIGraphicsBeginImageContextWithOptions(size, opaque, scale); 

यह आकर्षित करेगा है रेटिना और गैर रेटिना डिस्प्ले के लिए सही ढंग से आपके ग्राफिक्स।

FYI, UIGraphicsBeginImageContext(size)UIGraphicsBeginImageContextWithOptions(size, FALSE, 1.f) के समतुल्य है जो किसी भी रेटिना डिस्प्ले के लिए ठीक नहीं है जिसमें कुछ पारदर्शिता हो सकती है।

हालांकि, अगर आपको पारदर्शिता की आवश्यकता नहीं है, तो अपारदर्शी तर्क के लिए TRUE में पारित होने के लिए यह अधिक अनुकूलित है।

ड्राइंग का सबसे सुरक्षित और अनुशंसित तरीका स्केल तर्क के रूप में [[UIScreen mainScreen]scale] में पास करना है।

इसलिए उपरोक्त उदाहरण (रों) के लिए, तो आप इस बजाय प्रयोग करेंगे:

UIGraphicsBeginImageContextWithOptions(aSize, FALSE, [[UIScreen mainScreen] scale]); 

अधिक जानकारी के लिए, Apple's docs की जाँच करें।

+0

सरल, संक्षिप्त और सहायक स्पष्टीकरण; +1। –

10

आप UASiew में UIBezierPath को CAShapeLayer का उपयोग करके उप-वर्गीकरण के बिना भी जोड़ सकते हैं।

उदाहरण के लिए, एक UIView में केंद्रित एक 3PT सफेद लाइन के रूप में अपने पथ को जोड़ने के लिए:

UIBezierPath *mybezierpath = [UIBezierPath 
        bezierPathWithOvalInRect:CGRectMake(0, 0, 100, 100)]; 

CAShapeLayer *lines = [CAShapeLayer layer]; 
lines.path = mybezierpath.CGPath; 
lines.bounds = CGPathGetBoundingBox(lines.path); 
lines.strokeColor = [UIColor whiteColor].CGColor; 
lines.fillColor = [UIColor clearColor].CGColor; /*if you just want lines*/ 
lines.lineWidth = 3; 
lines.position = CGPointMake(self.myview.frame.size.width/2.0, self.myview.frame.size.height/2.0); 
lines.anchorPoint = CGPointMake(.5, .5); 

[self.myview.layer addSublayer:lines]; 
1

स्विफ्ट 2.0 में:

let path = UIBezierPath() 

let p1 = CGPointMake(0,self.view.frame.height/2) 
let p3 = CGPointMake(self.view.frame.width,self.view.frame.height/2) 

path.moveToPoint(p1) 
path.addQuadCurveToPoint(p3, controlPoint: CGPoint(x: self.view.frame.width/2, y: 0)) 

let line = CAShapeLayer() 
line.path = path.CGPath; 
line.strokeColor = UIColor.blackColor().CGColor 
line.fillColor = UIColor.redColor().CGColor 
view.layer.addSublayer(line) 
संबंधित मुद्दे