How to resize the uiview using touches on the corners of the uiview. For e.g. it top left corner is touched and dragged upwards its y coordinate and height should increase, if bottom right corner is dragged then its origin should be same but height and width should be changed.
2 Answers
4
votes
You can do it by changing the view.layer anchor point.
You can read about it here: Layer Geometry
To get the UIView corners you can use -
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.
Then You can cheek if the touch point is inside one of the corners. and set the layer.anchorPoint according.
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).
Then when you will resize the view it will resize from the anchor point.
Good Luck
1
votes
#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) {
}