Oleg
Oleg

Reputation: 1393

setting new frame after transform objective-c

I have a problem with setting new frame to uiview object. here is my code:

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

The NSLog output is not desirable :(

values of new rect: newOriginY: 78 newFrameHeight: 77.82

but NSLog is different: blockImage frame y: 85.0665 blockImage frame height: 70.7535

Upvotes: 1

Views: 2497

Answers (1)

mattjgalloway
mattjgalloway

Reputation: 34912

If you're applying a transform then the frame property should be ignored. You should set the bounds and the center appropriately. The documentation for UIView states this.

Upvotes: 6

Related Questions