2012-01-03 67 views
2

如何使用uiview角落的觸摸調整uiview的大小。對於例如它的左上角被觸摸並向上拖動它的y座標並且高度應該增加,如果右下角被拖動,那麼它的原點應該是相同的,但是高度和寬度應該被改變。從其角落調整uiview的尺寸

回答

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). 

然後,當您將調整它會從錨點調整視圖。

好運

+0

我寫上面的線接觸開始,運行程序,但是當我觸摸和拖動點什麼也沒有發生。 – 2012-01-03 10:55:40

+0

如何調整視圖大小。是調整大小方法相同的所有四個角落 – 2012-01-03 11:24:38

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) { 
} 

+0

謝謝,但如何調整從角落,什麼將觸摸移動新的座標。就像拖動你寫的rect.orgin.x + =(end.x-start.x)。什麼是每個角落的代碼。謝謝 – 2012-01-06 04:21:14