2012-11-07 34 views
5

में केंद्रित नहीं होगा मेरे पास UIImageView है जो मैं उस क्षेत्र को बढ़ाने के लिए UIView में जोड़ता हूं जहां स्पर्श पहचाने जाते हैं। जब मैं UIView (नीचे कोड) में UIImageView को केन्द्रित करने का प्रयास करता हूं तो केंद्र ठीक से सेट होता है लेकिन जब मैं प्रोग्राम चलाता हूं तो छवि UIView के फ्रेम के बाहर दिखाई देती है।UIImageview पर्यवेक्षण

कोई विचार?

// image size is 32x32 pixels but view will be created with 44x44 so touch recognition is better 
self = [super initWithFrame:CGRectMake(position.x, position.y, 44, 44)]; 

if (self) { 
    switch (color) { 
     case Blue: 
      self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pin_red"]]; 
      break; 
     case Green: 
      self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pin_green"]]; 
     default: 
      break; 
    } 

    [self setCenter:position]; 
    [self addSubview:self.imageView]; 
    [self setExclusiveTouch:YES];   
    [self setBackgroundColor:[UIColor clearColor]]; 

    [self.imageView setCenter:position]; 

    NSLog(@"uiview center: %@", NSStringFromCGPoint(self.center)); 
    NSLog(@"image center: %@", NSStringFromCGPoint(self.imageView.center)); 

धन्यवाद!

उत्तर

20

छविदृश्य सेट केंद्र अपने माता-पिता के सापेक्ष है। प्रयास करें:

[self.imageView setCenter:CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2)]; 
+0

त्वरित जवाब के लिए धन्यवाद है, लेकिन अगर मैं कोशिश [self.imageView setCenter: self.center] नतीजा वही है :-( – illogic

+1

जो मैं सुझाव दे रहा था वह नहीं था। आपको एक बिंदु प्राप्त करने की आवश्यकता है जो माता-पिता के सापेक्ष है। self.center इसे अपने माता-पिता के सापेक्ष वापस कर देगा, और – tillerstarr

+0

पर काम नहीं करना चाहिए, लेकिन स्वयं पर्यवेक्षक है और इसलिए यह इस मामले में अभिभावक है। या मैं गलत हूँ? – illogic

0

UIImageView फ्रेम मीटर फ़ाइल में ज फ़ाइल

UIImageView *imgCenter; 

2) में UIView

1) के केंद्र देने के लिए इस कोड की कोशिश

- (void)viewDidLoad 
{ 
    imgCenter=[[UIImageView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2, 35, 35)]; 
    [imgCenter setImage:[UIImage imageNamed:@"centerImage.png"]]; 
    [self.view addSubview:imgCenter]; 
} 

या

- (void)viewDidLoad 
{ 
    imgCenter=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 35, 35)]; 
    imgCenter.center = self.view.center; 
    [imgCenter setImage:[UIImage imageNamed:@"centerImage.png"]]; 
    [self.view addSubview:imgCenter]; 
} 
1
UIImageView *imageforFace=[[UIImageView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height/2, 125,125)]; 
imageforFace.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin; 
[imageforFace setCenter:CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2)]; 
imageforFace.image=[UIImage imageNamed:@"face.png"]; 
[self.view addSubview:imageforFace]; 

अंतिम उत्पादन

enter image description here