【问题标题】:How do i resize UIView with respect to ratio on dragging the corner edges?如何在拖动角边缘时根据比例调整 UIView 的大小?
【发布时间】:2012-12-16 02:18:11
【问题描述】:

我有一个大小不一的 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);
    }

}

【问题讨论】:

    标签: iphone ios ipad uiview


    【解决方案1】:

    你可以试试这个:

    检查deltaWidthdeltaHeight 中哪个绝对值更大。它成为前导大小并按原样应用于框架。另一个是根据比率从前导大小 delta 计算出来的,然后应用到帧上,而忽略实际(较小)的 delta。

    您可能还需要在初始 deltaWidthdeltaHeight 中应用比率检查,以便更好地确定哪个变化更大。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-12
      • 1970-01-01
      • 2011-12-07
      • 1970-01-01
      相关资源
      最近更新 更多