iphoneuiviewuianimationsetbounds

setBound width and height


This method gets called after i resize a photo:

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

It rounds the photo size; a width of 66 will be rounded to 50, 178 to 200 etc. It works fine when i use the setFrame method, but because im also working with rotation i want to work with setBounds.

When i use setBounds, the view will resize, but instead of resizing to a rounded number like 300, it resized to something random as 311.23.

Am i missing something here?


Solution

  • The answer is that i have to use the

    [view setTransform:CGAffineTransformIdentity];
    

    Now its using the already 'used' matrix and multiplies it with that.

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