setBound的宽度和高度

时间:2012-04-05 10:25:58

标签: iphone uiview uianimation setbounds

调整照片大小后调用此方法:

- (void)correctSize:(UIView*)view{
    //check if the newWidth is rounded by 50
    CGFloat correctWidth = roundf(_lastScale*_lastWidth/XPCollageGridHorizontalStepSize) * XPCollageGridHorizontalStepSize;
    CGFloat correctHeight = roundf(_lastScale*_lastHeight/XPCollageGridVerticalStepSize) * XPCollageGridVerticalStepSize;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5f]; 
    [view setBounds:CGRectMake(view.bounds.origin.x, view.bounds.origin.y, correctWidth, correctHeight)];
    //[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, correctWidth, correctHeight)];
    [UIView commitAnimations];
}

它使照片尺寸变圆;宽度为66将四舍五入为50,178至200等。 当我使用setFrame方法时它工作正常,但因为我也在使用旋转我想使用setBounds。

当我使用setBounds时,视图将调整大小,但不是调整为像300这样的舍入数字,而是将其调整为随机的值311.23。

我在这里错过了什么吗?

2 个答案:

答案 0 :(得分:1)

尝试使用以下内容:

// see the values here
NSLog(@"%f", correctWidth);
NSLog(@"%f", correctHeight);
view.frame = CGRectMake(view.bounds.origin.x, view.bounds.origin.y, correctWidth, correctHeight);
// see the values given
NSLog(@"%f", view.frame.size.width);
NSLog(@"%f", view.frame.size.height);

只是一个想法,不确定它是否会起作用

答案 1 :(得分:0)

答案是我必须使用

[view setTransform:CGAffineTransformIdentity];

现在它使用已经“使用过的”矩阵并将其与之相乘。

    CGFloat correctWidth = roundf(_lastScale*_lastWidth/XPCollageGridHorizontalStepSize) * XPCollageGridHorizontalStepSize;
CGFloat correctHeight = roundf(_lastScale*_lastHeight/XPCollageGridVerticalStepSize) * XPCollageGridVerticalStepSize;

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5f]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[view setTransform:CGAffineTransformIdentity]; //!!this line is added!
[view setBounds:CGRectMake(0, 0, correctWidth, correctHeight)];

[UIView commitAnimations];