Search code examples
uiviewiphone-4dynamic-resizing

uiview resizing and manipulation problem for iphone 4G


I have written a code that I use for cropping purpose. The scenario is like, It has 4 uiviews

1) Top
2) Left
3) Right
4) Bottom

Each uiview is resized according to the desired change and the main uiview has a uiimageview which contains an image. when I run the code on iPhone 3, 3GS, iPad 2, iPad 4 it works fine but when i run it on iPhone 4G, it generates very undesired result. I know my code calculations are fine thats why they are working fine on every device except the iPhone 4G. What will the issue be ? Any ideas ? Is uiview calculations are different for iPhone 4 or any other reason ?

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    //    messageLabel.text = @"Drag Detected";

commonCropT = leftRect.frame.origin.y; // left.y or Right.y or Top.H is same thing
commonCropB = lowerRect.frame.origin.y;// left.H or Right.H or lower.y is same thing    
commonCropL = leftRect.frame.size.width; //
commonCropR = rightRect.frame.origin.x;//
commonCropH = leftRect.frame.size.height;// left.width and right.width

leftYT = leftRect.frame.origin.y;
leftH = leftRect.frame.size.height;
leftYB = leftYT + leftH;
leftW = leftRect.frame.size.width; // leftXR


rightXL = rightRect.frame.origin.x;
rightW = rightRect.frame.size.width;
rightXR = rightXL + rightW;
rightYT = rightRect.frame.origin.y; 
rightH = rightRect.frame.size.height;
rightYB = rightH + rightYT;

if (isTappedOnUpperLeftCorner)
{
    if (commonCropR - movedPoint.x <= kWidthDifference)
        return;

    if (movedPoint.y < commonCropT )
    {
        commonCropH  = commonCropH + (commonCropT - movedPoint.y); 
    }
    else if (movedPoint.y > commonCropT )
    {
        if (commonCropB - movedPoint.y <= kHeightDifference ) {
            return;
        }
        commonCropH  = commonCropH - (movedPoint.y - commonCropT); 
    }

    commonCropL = movedPoint.x;
    commonCropT = movedPoint.y;

    NSLog(@" cropW = %d ",cropW);

    [upperRect setFrame:CGRectMake(upperRect.frame.origin.x, upperRect.frame.origin.y, upperRect.frame.size.width, commonCropT)];
    [leftRect setFrame:CGRectMake(leftRect.frame.origin.x, commonCropT, commonCropL, commonCropH)]; 
    [rightRect setFrame:CGRectMake(commonCropR, commonCropT, rightRect.frame.size.width, commonCropH)];  

}

}


Solution

  • I believe the problem you're running into is similar to a problem I'm seeing. With the retina display the touch coordinates may be non-integral, e.g. 23.5. If you use non-integral value with the frame of a view you may run into this kind of behavior. It appears that UIView doesn't support non-integral values.

    Use roundf() or a similar function at some point in your calculations to avoid this problem.