2012-03-03 62 views
0

我做了一個UIView子類(碗),當我嘗試做一個大碗內的碗,然後集中在外層一箇中心內一個它最終得到下方和安置中心右側。我將如何正確地集中它?需要幫助正確定位的UIView在一個UIView

- (id)initWithFrame:(CGRect)frame 
{ 
self = [super initWithFrame:frame]; 
if (self) { 
    // Initialization code 
    //CGColorRef lightGrayColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0].CGColor; 
    self.userInteractionEnabled = YES; 
    UIGestureRecognizer *twoFingerZ = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchZoom:)] ; 
    [self addGestureRecognizer:twoFingerZ]; 
    UILabel *label = [[UILabel alloc] init]; 
    [label setFont:[UIFont fontWithName:@"Arial" size:64]]; 
    label.text = [NSString stringWithFormat:@"March"]; 
    [label sizeToFit]; 
    label.center = self.center; 
    label.backgroundColor = [UIColor clearColor]; 
    label.textColor = [UIColor lightGrayColor]; 
    self.backgroundColor = [UIColor whiteColor]; 
    [self addSubview:label]; 
} 
return self; 
} 

-(void)pinchZoom:(UIPinchGestureRecognizer *)recognizer 
{ 
if([(UIPinchGestureRecognizer*)recognizer state] == UIGestureRecognizerStateEnded) { 
CGAffineTransform newForm = CGAffineTransformMakeScale(600/self.bounds.size.width, 600/self.bounds.size.height); 

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.5]; 
[self setTransform:newForm]; 
    return; 
[UIView commitAnimations]; 
} 

if([(UIPinchGestureRecognizer*)recognizer state] == UIGestureRecognizerStateBegan) { 
    subClip = [[clipView alloc] initWithFrame:CGRectMake((self.center.x - ([delegate getSize:self].width/2)), (self.center.y - ([delegate getSize:self].height/2)), [delegate getSize:self].width, [delegate getSize:self].height)]; 
    subClip.transform = CGAffineTransformMakeScale(0.1, 0.1); 
    [self addSubview:subClip]; 

    bowl *newB = [[bowl alloc] initWithFrame:CGRectMake(0, 0, 400, 400)]; 
    newB.center = self.center; 
// right here! ************* 
    [self addSubview:newB]; 
} 
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale); 
recognizer.scale = 1;  
} 

編輯:我修好了。顯然,對於一個UIView中心值是不正是我認爲這是(它看起來像它的另一種觀點認爲什麼的中心位置),所以不是newB.center = self.center,以正確的方式做到這一點是newB.center = CGPointMake(self.frame.size.width/2,self.frame.size.height/2);

+0

你能正確地做到這一點沒有動畫。 – Ravin 2012-03-03 06:43:42

+0

不,就試了一下,它似乎是完全一樣的,只是沒有當然的動畫。 – 2012-03-03 06:50:26

+0

根據我的理解,自己是你的外碗,而newB是內碗?如果你能提供兩個碗的框架尺寸,這將是有幫助的。 – Ravin 2012-03-03 06:56:43

回答

2

要居中,你應該能夠使用內層標籤:

label.center=CGPointMake(self.center.x,self.center.y); 

但首先你可能會想限定內標籤的幀大小:

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,yourWidth,yourHeight); 

您可能還想要將文本居中在內部標籤中:

label.textAlignment = UITextAlignmentCenter; 

此外,不確定您打算在y我們使用此代碼:

[label sizeToFit]; 

according to the documentation,sizeToFit將調整,所以它圍繞它的子視圖移動接收器視圖。在這種情況下,您的內部視圖不會顯示子視圖。所以你的對中可能會失敗,因爲你的內在標籤可能有意想不到的尺寸。嘗試使用該標籤的背景色設置爲東西,你可以看到(即不[UIColor clearColor])仔細檢查其尺寸。可能有助於解決對中問題。

祝你好運。

+0

謝謝!這絕對有助於集中UILabel。然而,它並沒有真正幫助集中UIView,但我認爲我已經修復了。 – 2012-03-03 07:15:34

+0

沒問題。很高興你修好了它。這個地方非常適合。 – crgt 2012-03-03 07:20:20

+0

將它標記爲@AlCapwn的正確答案 – 2012-03-04 20:33:45