我做了一個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);
你能正確地做到這一點沒有動畫。 – Ravin 2012-03-03 06:43:42
不,就試了一下,它似乎是完全一樣的,只是沒有當然的動畫。 – 2012-03-03 06:50:26
根據我的理解,自己是你的外碗,而newB是內碗?如果你能提供兩個碗的框架尺寸,這將是有幫助的。 – Ravin 2012-03-03 06:56:43