2012-07-18 95 views
0

我一直在嘗試下面的代碼,以交叉兩個UIView與anchorPoint(1,0)。兩個UIView具有相同的錨點

UIView *v=[[UIView alloc] initWithFrame:CGRectMake(50,50, 100, 100)]; 
v.backgroundColor=[UIColor redColor]; 
v.layer.anchorPoint=CGPointMake(1,0); 

UIView *v2=[[UIView alloc] initWithFrame:CGRectMake(50,50,200,300)]; 
v2.backgroundColor=[UIColor greenColor]; 
v2.layer.anchorPoint=CGPointMake(1, 0); 

[self.view addSubview:v2]; 
[self.view addSubview:v]; 

我對這段代碼的輸出如下所示。

Intersect Two Views with Anchor Point

我看起來像紅色視圖輸出的重疊綠色觀點與兩者具有相同的錨點,如下顯示。

enter image description here

+0

是上述代碼中'frame'值的正確輸出嗎?這張照片上的「P(50.f,50.f)」在哪裏?如果將'anchorPoint'行註釋掉會導致什麼結果? – holex 2012-07-18 15:36:10

回答

1

它可以幫助來形容什麼是你的榜樣發生的事情:

你把兩個矩形的確切大小的屏幕上準確位置(50, 50)(相對於它們在左上角的起源) 它們都具有默認情況下位於其中心的錨點。想象一下,在這些中心點放一針。 現在你說你實際上想要在右上角的引腳而不是中心。

一兩件事情都不會發生,要麼

1)的矩形將保持靜止,而引腳將自己移動到右上角的角落red:(150, 50)green:(250, 50),這是不是你想要的。

2)引腳將保持平穩和矩形將移動本身,直到他們的右上邊角其中引腳red pin:(100, 100)green pin:(150, 200),這也不是你想要的!

第二種選擇是實際發生的情況。

按照文檔的layer.position相對於layer.anchorPoint,所以你應該嘗試設置anchorPoint到頂部右上角,然後設置兩個矩形層的position到你想要的確切位置。例如

UIView *v=[[UIView alloc] initWithFrame:CGRectMake(50,50, 100, 100)]; 
v.backgroundColor=[UIColor redColor]; 
v.layer.anchorPoint=CGPointMake(1,0); 
v.layer.position = CGPointMake(200, 50); 

UIView *v2=[[UIView alloc] initWithFrame:CGRectMake(50,50,200,300)]; 
v2.backgroundColor=[UIColor greenColor]; 
v2.layer.anchorPoint=CGPointMake(1, 0); 
v2.layer.position = CGPointMake(200, 50); 

[self.view addSubview:v2]; 
[self.view addSubview:v]; 

只是爲了好玩:這裏是一個動畫顯示的矩形從最初的位置如何獲得initWithFrame到它們的最終位置,相對於它們的中心錨點。

UIView *v=[[UIView alloc] initWithFrame:CGRectMake(50,50, 100, 100)]; 
v.backgroundColor=[UIColor redColor]; 

UIView *v2=[[UIView alloc] initWithFrame:CGRectMake(50,50,200,300)]; 
v2.backgroundColor=[UIColor greenColor]; 

[self.view addSubview:v2]; 
[self.view addSubview:v]; 

UIView *ap=[[UIView alloc] initWithFrame:CGRectMake(0,0, 10, 10)]; 
ap.backgroundColor=[UIColor blackColor]; 
ap.center = v.layer.position; 

UIView *ap2=[[UIView alloc] initWithFrame:CGRectMake(0,0, 10, 10)]; 
ap2.backgroundColor=[UIColor blackColor]; 
ap2.center = v2.layer.position; 

[self.view addSubview:ap]; 
[self.view addSubview:ap2]; 

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"anchorPoint"]; 
animation.duration = 2.0; 
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(1,0)]; 
animation.autoreverses = YES; 
animation.repeatCount = 10; 
[v.layer addAnimation:animation forKey:@"anchorPoint"]; 
[v2.layer addAnimation:animation forKey:@"anchorPoint"]; 
+0

添加了一個動畫來說明:-) – 2012-07-21 13:40:40

1

你的意思是,你喜歡紅色的觀點是綠色的觀點和紅原視圖之間的重疊面積的大小?

如果是這樣,

v.frame = CGRectIntersection(v.frame, v2.frame); 
+0

我不想檢查它是否相交。紅色圖像必須與彩色圖像相交,並帶有定位點(1,0) – andyPaul 2012-07-18 14:49:48

+0

對不起andyPaul,你將不得不更清楚你想要什麼。 CGRectInsersectsRect返回相交區域的CGRect。 – 2012-07-18 14:54:03

+0

抱歉,您錯了,請轉到此鏈接https://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CGGeometry/Reference/reference.html,CGRectIntersectsRect返回布爾 – andyPaul 2012-07-18 15:00:00

2

請檢查原始文檔有關anchorPoint和座標系here

圖2三個anchorPoint值:Three anchorPoint values

更新#1:

(此圖片顯示OSX的座標系統!)

+0

我已經檢查過了,不知道我錯在哪裏。 – andyPaul 2012-07-18 15:23:45

相關問題