contentsize、contentoffset和contentInset詳解

來源:互聯網
上載者:User

下面我用怎麼做一個書架的例子和大家說說contentsize、contentoffset 和contentInset的區別。

[plain]
NSString *path = nil; 
UIImage *back = nil; 
NSString *str = nil; 
UIImage *aImage = nil; 
 
path = [[NSBundle mainBundle] pathForResource:@"shujia" ofType:@"jpg"]; 
back = [UIImage imageWithContentsOfFile:path];//載入圖片 
 
_scrollView = [[UIScrollView  alloc] initWithFrame:CGRectMake(96, 84,back.size.width,back.size.height)]; 
_scrollView.showsVerticalScrollIndicator = NO;      //將豎直方向的捲軸隱藏  
 
//將shujia.jpg作為_scrollerView的背景 
UIColor *color=[[UIColor alloc]initWithPatternImage:back]; 
[_scrollView setBackgroundColor:color]; 
 
_buttonList=[NSMutableArray arrayWithCapacity:9]; 
for(int i=0;i<9;i++) 

    str=[NSString stringWithFormat:@"menubook%d",i+1]; 
    path = [[NSBundle mainBundle] pathForResource:str ofType:@"png"]; 
    aImage = [UIImage imageWithContentsOfFile:path]; 
    int x = (i%3)* (aImage.size.width+15); 
    int y = (i/3)* (aImage.size.height+30); 
    _whichPeople = [UIButton buttonWithType:UIButtonTypeCustom]; 
    _whichPeople.frame = CGRectMake(x+10,y+30,aImage.size.width,aImage.size.height); 
    _whichPeople.tag = i; 
    [_whichPeople setImage:aImage forState:UIControlStateNormal];//這個圖片是在布點擊時響應 
    path = nil; 
    aImage = nil; 
    str = nil; 
     
    str=[NSString stringWithFormat:@"menubook%d_1",i+1]; 
    path = [[NSBundle mainBundle] pathForResource:str ofType:@"png"]; 
    aImage = [UIImage imageWithContentsOfFile:path]; 
    [_whichPeople setImage:aImage forState:UIControlStateHighlighted];//圖片在點擊的時候響應 
    [_whichPeople addTarget:self action:@selector(ButtonClick:) forControlEvents:UIControlEventTouchUpInside]; 
    [_scrollView addSubview:_whichPeople]; 
    [_buttonList addObject:_whichPeople]; 
    path = nil; 
    aImage = nil; 
    str =  nil; 
     

 
//控制可以滾動的地區 
_scrollView.contentSize = CGSizeMake(480, 580);  
//當前顯示地區頂點相對於frame頂點的位移量 
_scrollView.contentOffset = CGPointMake(0, 0); 
//scrollview的contentview的頂點相對於scrollview的位置 
_scrollView.contentInset = UIEdgeInsetsMake(0 , 0, 0, 0); 
[self.view addSubview:_scrollView];  
contentSize是scrollview可以滾動的地區,比如frame = (0 ,0 ,320 ,480) contentSize = (320 ,960),代表你的scrollview可以上下滾動,捲動區域為frame大小的兩倍。
contentOffset是scrollview當前顯示地區頂點相對於frame頂點的位移量,比如上個例子你拉到最下面,contentoffset就是(0 ,480),也就是y位移了480
contentInset是scrollview的contentview的頂點相對於scrollview的位置,例如你的contentInset = (0 ,100),那麼你的contentview就是從scrollview的(0 ,100)開始顯示

 


摘自 ComeOnTom的專欄

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.