2013-04-08 7 views
39

के अंदर सापेक्ष स्थिति प्राप्त करने के लिए कनवर्टपॉइंट का उपयोग करके मैंने इस विषय पर एक दर्जन SO प्रश्नों को देखा है, और मेरे उत्तरों में से कोई भी मेरे लिए काम नहीं करता है। शायद यह मुझे सही रास्ते पर वापस लाने में मदद करेगा।माता-पिता UIView

इस स्थापना की कल्पना कीजिए:

enter image description here

मैं UIView करने के लिए UIButton रिश्तेदार के center निर्देशांक प्राप्त करना चाहते।

दूसरे शब्दों में, UIButton केंद्र UITableViewCell के भीतर 215, 80 हो सकता है, लेकिन UIView के सापेक्ष वे 260, 165 की तरह होना चाहिए। मैं दोनों के बीच कैसे परिवर्तित करूं?

यहाँ मैं क्या करने की कोशिश की है या नहीं:

[[self.view superview] convertPoint:button.center fromView:button]; // fail 
[button convertPoint:button.center toView:self.view]; // fail 
[button convertPoint:button.center toView:nil]; // fail 
[button convertPoint:button.center toView:[[UIApplication sharedApplication] keyWindow]]; // fail 

मैं इसे बटन के superviews सभी के माध्यम से पाशन और ऊपर x और y निर्देशांक जोड़कर मुश्किल तरीके से कर सकता है, लेकिन मुझे लगता है कि overkill है। मुझे सिर्फ covertPoint सेटिंग्स का सही संयोजन खोजने की आवश्यकता है। सही?

+0

आप जोड़ना यदि आपका प्रश्न के लिए निम्न यह मदद मिलेगी: अपनी छवि में सभी 4 विचारों के फ्रेम, उत्पादन आप तरीकों से प्राप्त कर आप की कोशिश की है, और आप मूल्य वास्तव में चाहते हैं। – rmaddy

उत्तर

86

button.center केंद्र अपने superview की समन्वय प्रणाली में निर्दिष्ट है, इसलिए मुझे लगता है कि निम्नलिखित काम करता है:

CGPoint p = [button.superview convertPoint:button.center toView:self.view] 

या आप अपने स्वयं में बटन के केंद्र की गणना समन्वय प्रणाली और उस का उपयोग करें:

CGPoint buttonCenter = CGPointMake(button.bounds.origin.x + button.bounds.size.width/2, 
            button.bounds.origin.y + button.bounds.size.height/2); 
CGPoint p = [button convertPoint:buttonCenter toView:self.view]; 

स्विफ्ट 3

var p = button.convertPoint(button.center, to: self.view) 
+2

बिंगो! आपका पहला प्रयास वह था जिसे मैं ढूंढ रहा था। पर्यवेक्षण की समन्वय प्रणाली से जुड़ा केंद्र वह है जो मैं खो रहा था। धन्यवाद! – Axeva

+0

धन्यवाद @ मार्टिन आर। यह सहायक था –

+1

** स्विफ्ट 3: ** 'var बटन केंद्र = CGPoint (x: button.bounds.origin.x + button.bounds.size.width/2, y: button.bounds.origin.y + button.bounds .size.height/2) var p = बटन।कन्वर्टपॉइंट (बटन सेंटर, से: self.view) ' –

10

मार्टिन उत्तर सही है। स्विफ्ट का उपयोग कर डेवलपर्स के लिए, आप किसी ऑब्जेक्ट की स्थिति प्राप्त कर सकते हैं (बटन, देखने, ...) का उपयोग करके स्क्रीन के सापेक्ष:

var p = obj.convertPoint(obj.center, toView: self.view) 

println(p.x) // this prints the x coordinate of 'obj' relative to the screen 
println(p.y) // this prints the y coordinate of 'obj' relative to the screen 
+0

@ एक्सेवा अधिक सटीक गणना के लिए 'कन्वर्ट्रैक' भी है [यहां] (http://stackoverflow.com/a/33879565/1634890) –

1
तेज 2.2 में

मेरे लिए काम किया:

var OrignTxtNomeCliente:CGPoint! 

if let orign = TXT_NomeCliente.superview, let win = UIApplication.sharedApplication().keyWindow { 
     OrignTxtNomeCliente = orign.convertPoint(TXT_NomeCliente.frame.origin, toView: win) 
    } 
2

यहां स्विफ्ट 3 @ पाब्लो के उत्तर का अपडेट है, जिसने मेरे मामले में बहुत अच्छा काम किया है।

if let window = UIApplication.shared.keyWindow { 
    parent.convert(child.frame.origin, to: window) 
} 
0

स्विफ्ट 4

आप बटन, नहीं superview से convert कॉल करने के लिए की जरूरत है। मेरे मामले में मुझे चौड़ाई डेटा की आवश्यकता थी इसलिए मैंने केवल केंद्र बिंदु की बजाय सीमाएं परिवर्तित कीं। नीचे दिए गए कोड मेरे लिए काम करता है:

let buttonAbsoluteFrame = button.convert(button.bounds, to: self.view)