在变换objective-c之后设置新帧

时间:2011-11-28 09:48:15

标签: iphone objective-c uiview frame

我将新框架设置为uiview对象时遇到问题。 这是我的代码:

UIImageView *blockImage=[[UIImageView alloc] initWithFrame:CGRectMake(0, [y doubleValue], self.view.frame.size.width, 86)];

[blockImage setImage:[UIImage imageNamed:@"mainscreen_block_sample.jpg"]];
[blockImage setBackgroundColor:[UIColor blackColor]];


//setting anchor point
[blockImage.layer setAnchorPoint:CGPointMake(0.5, 1.0)];

//preparing transform
float distance = 500;
CATransform3D basicTrans = CATransform3DIdentity;
basicTrans.m34 = 1.0 / -distance;

//calculating angle
double OF=oldSize/2.0-difference;
double OC=oldSize/2.0;    
double angle= acosf(OF/OC);
angle=angle/2.0;

//transforming
blockImage.layer.transform = CATransform3DRotate(basicTrans, angle, 1.0f, 0.0f, 0.0f);

double newOriginY=[y doubleValue]-difference;
double newFrameHeight=blockImage.frame.size.height;

[blockImage setFrame:CGRectMake(0, newOriginY, blockImage.frame.size.width, newFrameHeight)];

NSLog(@"    blockImage frame y: %g", blockImage.frame.origin.y);
NSLog(@"    blockImage frame height:   %g", blockImage.frame.size.height);

//adding subview
[blocks addObject:blockImage];
[self.view addSubview:[blocks objectAtIndex:([blocks count]-1)]];

//releasing memory
[blockImage release];

不希望NSLog输出:(

新rect的值: newOriginY:78 newFrameHeight:77.82

但NSLog与众不同: blockImage frame y:85.0665 blockImage框架高度:70.7535

1 个答案:

答案 0 :(得分:6)

如果要应用变换,则应忽略frame属性。您应该适当地设置边界和中心。 UIView的文档说明了这一点。