frame bounds center
frame bounds center是View的屬性睬关,定義了View的位置和大小澎语。
frame bounds是一個(gè)結(jié)構(gòu)體肩祥,包含位置和帶下坞嘀。Center是一個(gè)坐標(biāo)郁油。
給一個(gè)View設(shè)置frame時(shí)本股,以父View的
struct CGRect // frame和bounds都屬于這一類型
{
CGPoint origin; //坐標(biāo)位置
CGSize size;//形狀大小
};
struct CGPoint // 坐標(biāo)位置中包含x坐標(biāo)和y坐標(biāo)
{
CGFloat x;
CGFloat y;
};
struct CGSize// 形狀大小指的是矩形的長(zhǎng)和寬
{
CGFloat width;
CGFloat height;
};
frame
frame設(shè)置了每個(gè)View相對(duì)于父View頂點(diǎn)的位置和自身的大小攀痊。
UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 250, 250)];
backView.backgroundColor = [UIColor redColor];
[self.view addSubview:backView];
UIView *midView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 150, 150)];
backView.backgroundColor = [UIColor greenColor];
[backView addSubview:midView];
UIView *frontView = [[UIView alloc] initWithFrame:CGRectMake(35, 35, 80, 80)];
frontView.backgroundColor = [UIColor blueColor];
[midView addSubview:frontView];
bounds
子View的frame會(huì)以父View的頂點(diǎn)為原點(diǎn),建立坐標(biāo)系拄显,計(jì)算子View的位置苟径。
當(dāng)給一個(gè)View設(shè)置bounds時(shí),會(huì)把自己當(dāng)作一個(gè)容器躬审。設(shè)置View的bounds棘街,會(huì)改變以自身定點(diǎn)的位置。
如下代承边。backView.bounds = CGRectMake(50, 50, 250, 250)表示對(duì)于midView來(lái)說(shuō)遭殉,backView的左上角坐標(biāo)變?yōu)椋?0,50)博助。因此险污,midView和backView的左上角對(duì)齊。
UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 250, 250)];
backView.backgroundColor = [UIColor redColor];
backView.bounds = CGRectMake(50, 50, 250, 250);
[self.view addSubview:backView];
UIView *midView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 150, 150)];
midView.backgroundColor = [UIColor greenColor];
[backView addSubview:midView];
UIView *frontView = [[UIView alloc] initWithFrame:CGRectMake(35, 35, 80, 80)];
frontView.backgroundColor = [UIColor blueColor];
[midView addSubview:frontView];
因此上面代碼執(zhí)行的結(jié)果是
center
center.x = frame.origin.x + frame.size.width / 2
center.y = frame.origin.y + frame.size.height / 2
總結(jié)
frame設(shè)置自身相對(duì)于父View的位置和自身大小富岳。
bounds設(shè)置子View的位置和自身的大小
contentSize contentInset contentOffset
contentSize contentInset contentOffset是scrollView的三個(gè)屬性
1.contentSize
The size of the content view. 這個(gè)size表示滾動(dòng)視圖可以滾動(dòng)的大小蛔糯,也就是scrollView內(nèi)容的大小。如果scrollView.contentSize大于scrollView.frame.size窖式,則有滑動(dòng)效果蚁飒。小于或等于滾動(dòng)視圖的frame.size,這時(shí)候滾動(dòng)視圖是不可以滾動(dòng)的萝喘,連橡皮筋效果都沒(méi)有淮逻。
假如frame = (0 ,0 ,320 ,480) contentSize = (640 ,480),代表你的scrollview可以橫向滾動(dòng)320的寬度蜒灰。
CGFloat scrollViewWidth = 300;
CGFloat scrollViewHeight = 500;
CGFloat pageWidth = 200;
CGFloat pageHeight = 500;
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, pageWidth, pageHeight)];
view1.backgroundColor = [UIColor redColor];
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(pageWidth, 0, pageWidth, pageHeight)];
view2.backgroundColor = [UIColor greenColor];
UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(pageWidth * 2, 0, pageWidth, pageHeight)];
view3.backgroundColor = [UIColor blueColor];
self.scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake((self.view.frame.size.width - scrollViewWidth) / 2, 50, scrollViewWidth, scrollViewHeight)];
self.scrollview.scrollEnabled = YES;
self.scrollview.pagingEnabled = NO;
self.scrollview.contentSize = CGSizeMake(3 * pageWidth, pageHeight);
[self.scrollview addSubview:view1];
[self.scrollview addSubview:view2];
[self.scrollview addSubview:view3];
[self.view addSubview:self.scrollview];
2.contentOffset
contentOffset是scrollView的contentView的頂點(diǎn)相對(duì)于frame定點(diǎn)的偏移量弦蹂。
CGFloat scrollViewWidth = 300;
CGFloat scrollViewHeight = 500;
CGFloat pageWidth = 200;
CGFloat pageHeight = 500;
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, pageWidth, pageHeight)];
view1.backgroundColor = [UIColor redColor];
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(pageWidth, 0, pageWidth, pageHeight)];
view2.backgroundColor = [UIColor greenColor];
UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(pageWidth * 2, 0, pageWidth, pageHeight)];
view3.backgroundColor = [UIColor blueColor];
self.scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake((self.view.frame.size.width - scrollViewWidth) / 2, 50, scrollViewWidth, scrollViewHeight)];
self.scrollview.contentOffset = CGPointMake(pageWidth, 0);
self.scrollview.scrollEnabled = YES;
self.scrollview.pagingEnabled = NO;
self.scrollview.contentSize = CGSizeMake(3 * pageWidth, pageHeight);
[self.scrollview addSubview:view1];
[self.scrollview addSubview:view2];
[self.scrollview addSubview:view3];
[self.view addSubview:self.scrollview];
3.contentInset
可以看做是給contentView四周設(shè)了一圈范圍肩碟,top,left正數(shù)增大其范圍强窖,top,left負(fù)數(shù)縮小其范圍)畫(huà)個(gè)圖可能更容易理解
CGFloat scrollViewWidth = 300;
CGFloat scrollViewHeight = 500;
CGFloat pageWidth = 200;
CGFloat pageHeight = 500;
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, pageWidth, pageHeight)];
view1.backgroundColor = [UIColor redColor];
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(pageWidth, 0, pageWidth, pageHeight)];
view2.backgroundColor = [UIColor greenColor];
UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(pageWidth * 2, 0, pageWidth, pageHeight)];
view3.backgroundColor = [UIColor blueColor];
self.scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake((self.view.frame.size.width - scrollViewWidth) / 2, 50, scrollViewWidth, scrollViewHeight)];
self.scrollview.contentInset = UIEdgeInsetsMake(30, 40, 50, 60);// 相當(dāng)于可滾動(dòng)區(qū)域上邊增加30,左邊增加40削祈,底邊增加50翅溺,右邊增加60
// 可滾動(dòng)范圍是 -30 -> contentSize.height + 50 -40 -> contentSize.width + 60
self.scrollview.scrollEnabled = YES;
self.scrollview.pagingEnabled = NO;
self.scrollview.contentSize = CGSizeMake(3 * pageWidth, pageHeight);
[self.scrollview addSubview:view1];
[self.scrollview addSubview:view2];
[self.scrollview addSubview:view3];
[self.view addSubview:self.scrollview];