如何根据拖动角落边缘的比例调整UIView的大小?

时间:2013-01-01 10:49:27

标签: iphone ios ipad uiview

我有一个大小的UIView。我正在点击按钮调整该视图的大小。但是UIView的大小调整取决于比例如3:2,4:3,16:9。而且必须根据比例来拖动UIView。

kResizeThumbSize 30

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
  {
    UITouch *touch = [[event allTouches] anyObject];

  if ([((UIView *)(touch.view)) isEqual:canvas])
    {
        touchStart = [[touches anyObject] locationInView:canvas];
        isResizingLR = (canvas.bounds.size.width - touchStart.x < kResizeThumbSize && canvas.bounds.size.height - touchStart.y < kResizeThumbSize);
        isResizingUL = (touchStart.x <kResizeThumbSize && touchStart.y <kResizeThumbSize);
        isResizingUR = (canvas.bounds.size.width-touchStart.x < kResizeThumbSize && touchStart.y<kResizeThumbSize);
        isResizingLL = (touchStart.x <kResizeThumbSize && canvas.bounds.size.height -touchStart.y <kResizeThumbSize);
    }

    if(isResizingLR || isResizingUL|| isResizingLL || isResizingUR){
        [canvas removeGestureRecognizer:panRecognizer];
    }
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    CGPoint touchPoint = [[touches anyObject] locationInView:canvas];
    CGPoint previous=[[touches anyObject]previousLocationInView:canvas];

    float  deltaWidth = touchPoint.x-previous.x;
    float  deltaHeight = touchPoint.y-previous.y;

    if (isResizingLR) {
        canvas.frame = CGRectMake(canvas.frame.origin.x, canvas.frame.origin.y,touchPoint.x + deltaWidth, touchPoint.y + deltaWidth);
    }
    if (isResizingUL) {
        canvas.frame = CGRectMake(canvas.frame.origin.x + deltaWidth, canvas.frame.origin.y + deltaHeight, canvas.frame.size.width - deltaWidth, canvas.frame.size.height - deltaHeight);
    }
    if (isResizingUR) {
        canvas.frame = CGRectMake(canvas.frame.origin.x ,canvas.frame.origin.y + deltaHeight,  canvas.frame.size.width + deltaWidth, canvas.frame.size.height - deltaHeight);
    }
    if (isResizingLL) {
        canvas.frame = CGRectMake(canvas.frame.origin.x + deltaWidth ,canvas.frame.origin.y ,  canvas.frame.size.width - deltaWidth, canvas.frame.size.height + deltaHeight);
    }

}

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

查看deltaWidthdeltaHeight中的哪一个作为绝对值更大。它成为一个领先的大小,并按原样应用于框架。另一个根据比率从前导大小增量计算,然后应用于帧,忽略实际(较小)增量。

您可能还需要在初始deltaWidth再次deltaHeight中应用比率检查,以更好地确定哪个更改更大。