2

如何使用触摸 uiview 的角来调整 uiview 的大小。例如,它的左上角被触摸并向上拖动,它的 y 坐标和高度应该增加,如果右下角被拖动,那么它的原点应该相同但高度和宽度应该改变。

4

2 回答 2

4

您可以通过更改 view.layer 锚点来做到这一点。

你可以在这里阅读: 图层几何

要获得 UIView 角落,您可以使用 -

CGRect topLeftCorner = CGRectMake(CGRectGetMinX(self.view),CGRectGetMinY(self.view),20,20); //Will define the top-left corner of the view with 20 pixels inset. you can change the size as you wish.

CGRect topRightCorner  =  CGRectMake(CGRectGetMaxX(self.view),CGRectGetMinY(self.view),20,20); //Will define the top-right corner.

CGRect bottomRightCorner  =   CGRectMake(CGRectGetMinX(self.view),CGRectGetMaxY(self.view),20,20); //Will define the bottom-right corner.

CGRect bottomLeftCorner  = CGRectMake(CGRectGetMinX(self.view),CGRectGetMinY(self.view),20,20); //Will define the bottom-left corner.

然后,如果触摸点在其中一个角落内,您可以脸颊。并据此设置 layer.anchorPoint。

  BOOL isBottomLeft =  CGRectContainsPoint(bottomLeftCorner, point);
  if(isLeft) view.layer.anchorPoint = CGPoint(0,0);
   //And so on for the others (off course you can optimize this code but I wanted to make the explanation simple).

然后,当您调整视图大小时,它将从锚点调整大小。

祝你好运

于 2012-01-03T10:37:48.263 回答
1

#define TOUCH_OFFSET 20 //distance from rectangle edge where it can be touched

UITouch* touch = [... current touch ...];

CGRect rectagle = [... our rectangle ... ];
CGPoint dragStart = [touch previousLocationInView:self.view];
CGPoint dragEnd = [touch locationInView:self.view];

//this branch is not necessary if we let users resize the rectangle when they tap its border from the outside
if (!CGRectContainsPoint(rectangle, dragStart)) {
  return;
}

if (abs(dragStart.x - CGRectGetMinX(rectangle)) < TOUCH_OFFSET) {
   //modify the rectangle appropiately, e.g.
   rectangle.origin.x += (dragEnd.x - dragStart.x);
   rectangle.size.width -= (dragEnd.x - dragStart.x);

   //TODO: you have to handle situation when width is zero or negative - flipping the rectangle or giving it a minimum width
}
else if (abs(dragStart.x - CGRectGetMaxX(rectangle)) < TOUCH_OFFSET) {
}

if (abs(dragStart.y - CGRectGetMinY(rectangle)) < TOUCH_OFFSET) {
}
else if (abs(dragStart.y - CGRectGetMaxY(rectangle)) < TOUCH_OFFSET) {
}

于 2012-01-03T11:07:46.017 回答