बस सोचा कि मैं यह जोड़ूंगा कि आपको इसे 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 की जाँच करें।
मैं इस कोशिश की, लेकिन कुछ नहीं होता। मैं अपने कस्टम व्यू में ड्राइंग कैसे प्राप्त करूं? – joec
UIView सबक्लास बनाएं और वहां drawRect विधि लागू करें, यह मेरे लिए कम से कम – Vladimir
काम करता है यह पुराना है, लेकिन अगर यह किसी और की मदद करता है: मैं यह निर्दिष्ट करना भूल गया कि इंटरफ़ेस बिल्डर में मेरा दृश्य "MyView" (क्लास पहचान) – Mirkules