2013-02-26 124 views
6

में एक आकृति खींचना मैं एक विशिष्ट छवि के साथ UIImageView के अंदर कुछ मंडलियों को आकर्षित करने का प्रयास कर रहा हूं।UIImageView IOS

UIGraphicsBeginImageContext(self.view.bounds.size); 
CGContextRef contextRef = UIGraphicsGetCurrentContext(); 
CGContextSetLineWidth(contextRef, 2.0); 
CGContextSetStrokeColorWithColor(contextRef, [color CGColor]); 
CGRect circlePoint = (CGRectMake(coordsFinal.x, coordsFinal.y, 50.0, 50.0)); 

CGContextStrokeEllipseInRect(contextRef, circlePoint); 

UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

[photoView addSubview:image]; 

चक्र ठीक तैयार की है, लेकिन मैं PhotoView इसे करने के लिए एक मुखौटा के रूप में कार्य करना चाहते हैं: यह मैं क्या करने की कोशिश कर रहा था। तो अगर उदाहरण के लिए मैं UIImageView को एनीमेशन का उपयोग करके UIView से बाहर ले जाता हूं, तो मैं सर्कल को इसके साथ आगे बढ़ना चाहता हूं। महत्वपूर्ण यह तथ्य है कि निर्देशांक पूरी स्क्रीन के सापेक्ष हैं।

+0

एक कस्टम UIView, या एक व्युत्पन्न कस्टम UIImageView जैसा लगता है। – trumpetlicks

उत्तर

10

इसके बजाय कोर एनीमेशन की आकृति परत का उपयोग करें।

CAShapeLayer *circleLayer = [CAShapeLayer layer]; 
// Give the layer the same bounds as your image view 
[circleLayer setBounds:CGRectMake(0.0f, 0.0f, [photoView bounds].size.width, 
               [photoView bounds].size.height)]; 
// Position the circle anywhere you like, but this will center it 
// In the parent layer, which will be your image view's root layer 
[circleLayer setPosition:CGPointMake([photoView bounds].size.width/2.0f, 
            [photoView bounds].size.height/2.0f)]; 
// Create a circle path. 
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect: 
            CGRectMake(0.0f, 0.0f, 50.0f, 50.0f)]; 
// Set the path on the layer 
[circleLayer setPath:[path CGPath]]; 
// Set the stroke color 
[circleLayer setStrokeColor:[[UIColor redColor] CGColor]]; 
// Set the stroke line width 
[circleLayer setLineWidth:2.0f]; 

// Add the sublayer to the image view's layer tree 
[[photoView layer] addSublayer:circleLayer]; 

अब, अगर आप UIImageView कि इस परत में शामिल चेतन, परत इसके साथ आ जाएगा के बाद से यह एक बच्चे को परत है। और अब drawRect: ओवरराइड करने की आवश्यकता नहीं है।

+0

बहुत अच्छा लगता है, लेकिन CoordsFinal के बारे में क्या है जो सर्कल की स्थिति निर्धारित करता है? – user2014474

+0

अब आप इसकी गणना कैसे कर रहे हैं? एक ही तकनीक का प्रयोग करें। यह वही होना चाहिए। आपके द्वारा बनाए गए प्रत्येक सर्कल/आकृति परत के लिए बस 'स्थिति' प्रॉपर्टी सेट करें। –

+0

CoordsFinal इस विधि से एक स्वतंत्र है CGPoint – user2014474

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

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