2012-01-03 14 views
2

uiviewのコーナーのタッチを使用してuiviewのサイズを変更する方法。例えば、上の左隅がタッチされ、y座標が上にドラッグされ、下の右隅がドラッグされている場合は原点は同じである必要がありますが、高さと幅は変更する必要があります。隅からのビューのサイズ変更

答えて

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. 

次にすることはでき頬タッチポイントはコーナーの1内にある場合。 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

私はタッチで上記の行が始まると、プログラムを実行しますが、私は触れると、そのポイントをドラッグすると何も起こりません書きました。 –

+0

ビューのサイズを変更するにはどうすればいいですか? 4つのコーナーすべてで同じサイズ変更方法ですか? –

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)と書いてあります。各コーナーのコードは何になりますか。 –

関連する問題