網頁

2012年6月4日 星期一

frame & bounds

此圖為網路截取

view的觀念

座標系中比較常用的幾個概念是
Frame:Frame指定了View相對於其父View座標系的位置和大小
Bounds:Bounds則是view相對於自身坐標系的位置和大小,bounds的起點通常都是(0,0),但是有些時候也可以透過這個屬性來對view的顯示範圍進行更靈活的控制。
Center:顧名思義就是view的Frame的中心。

frame的CGPoint是指父類view的原點坐標位置
而bounds則是只左上角自身坐標系的位置

bounds & frame的程式範例,可看到....frame & bounds之間的關係....
 
 UIView *v1 = [[UIView alloc] initWithFrame:CGRectMake(101.0f, 100.0f, 51.0f, 50.0f)];
 v1.backgroundColor = [UIColor yellowColor];
    
 NSLog(@"frame_x : %f, frame_y : %f",v1.frame.origin.x,v1.frame.origin.y);
 NSLog(@"frane_w : %f, frame_h : %f",v1.frame.size.width,v1.frame.size.height);
    
 NSLog(@"bounds_x : %f,bounds_y : %f",v1.bounds.origin.x,v1.bounds.origin.y);
 NSLog(@"bounds_w : %f,bounds_h : %f",v1.bounds.size.width,v1.bounds.size.height);
    
 [self.view addSubview:v1]; 

顯示出來的結果為
2012-06-11 10:09:33.064 TAAZA[489:f803] frame_x : 101.000000, frame_y : 100.000000
2012-06-11 10:09:33.065 TAAZA[489:f803] frane_w : 51.000000, frame_h : 50.000000
2012-06-11 10:09:33.065 TAAZA[489:f803] bounds_x : 0.000000,bounds_y : 0.000000
2012-06-11 10:09:33.066 TAAZA[489:f803] bounds_w : 51.000000,bounds_h : 50.000000

最後你可以試試在一個view上面addSubView疊加一個view後,用super.view.frame.origin.x等等之類的,看可否得知第一個view的frame的x位置 同理可證使用其他的位置去抓出父view的位置或者size

沒有留言:

張貼留言