从角落调整uiview的大小

时间:2012-01-03 09:21:00

标签: objective-c ios cocoa-touch uiview touches

如何使用uiview角落的触摸来调整uiview的大小。对于例如触摸左上角并向上拖动其y坐标和高度应增加,如果拖动右下角,则其原点应相同,但应更改高度和宽度。

2 个答案:

答案 0 :(得分:4)

您可以通过更改view.layer定位点来完成此操作。

你可以在这里阅读: Layer Geometry

要获得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).

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

祝你好运

答案 1 :(得分: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) {
}