2012-11-07 29 views
5

Tôi có UIImageView mà tôi thêm vào UIView để nâng cao khu vực nơi chạm được nhận dạng. Khi tôi cố gắng để trung tâm UIImageView trong UIView (mã dưới đây) trung tâm được thiết lập đúng nhưng khi tôi chạy chương trình hình ảnh được thể hiện cách tắt bên ngoài khung của UIView.UIImageview sẽ không tập trung trong superview

Bất kỳ ý tưởng nào?

// 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)); 

Cảm ơn!

Trả lời

20

Tập hợp tưởng tượngTrung tâm liên quan đến cha mẹ của nó. Hãy thử:

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

Cảm ơn câu trả lời nhanh nhưng nếu tôi cố gắng [self.imageView setCenter: self.center] kết quả là như nhau :-( – illogic

+1

Đó không phải những gì tôi đã đề xuất. Bạn cần phải có được một điểm đó là tương đối so với phụ huynh. self.center sẽ trả về nó tương ứng với cha mẹ của nó, và không nên làm việc – tillerstarr

+0

nhưng tự là superview và do đó, nó là cha mẹ trong trường hợp này. Hoặc là tôi sai? – illogic

0

thử mã này để cung cấp cho khung UIImageView Trung tâm UIView

1) trong file .h

UIImageView *imgCenter; 

2) trong file .m

- (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]; 
} 

HOẶC

- (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]; 

cuối cùng đầu ra là

enter image description here

Các vấn đề liên quan