iOS開發UI篇—UIScrollView控制項介紹

來源:互聯網
上載者:User

標籤:

一、知識點簡單介紹

  1.UIScrollView控制項是什嗎?

(1)行動裝置的螢幕?大?小是極其有限的,因此直接展?示在?使用者眼前的內容也相當有限

(2)當展?示的內容較多,超出?一個螢幕時,?使用者可通過滾動?手勢來查看螢幕以外的內容

(3)普通的UIView不具備滾動功能,不能顯?示過多的內容

(4)UIScrollView是一個能夠滾動的視圖控制項,可以?用來展?示?大量的內容,並且可以通過滾 動查看所有的內容

 (5)  舉例:手機上的“設定”、其他?樣本程式 

2.UIScrollView的簡單使用

(1)將需要展?的內容添加到UIScrollView中 

(2)設定UIScrollView的contentSize屬性,告訴UIScrollView所有內容的尺?寸,也就是告訴 它滾動的範圍(能滾多遠,滾到哪?裡是盡頭) 

  3.屬性

    (1)常用屬性:

      1)@property(nonatomic)CGPointcontentOffset; 這個屬性?用來表?示UIScrollView滾動的位置

      2)@property(nonatomic)CGSizecontentSize;這個屬性?用來表?示UIScrollView內容的尺?寸,滾動範圍(能滾多遠)

      3)@property(nonatomic)UIEdgeInsetscontentInset; 這個屬效能夠在UIScrollView的4周增加額外的捲動區域 

    (2)其他屬性:

      1)@property(nonatomic) BOOL bounces;  設定UIScrollView是否需要彈簧效果 

      2)@property(nonatomic, getter=isScrollEnabled) BOOL scrollEnabled; 設定UIScrollView是否能滾動 

      3)@property(nonatomic) BOOL showsHorizontalScrollIndicator; 是否顯?示?水平捲軸 

      4)@property(nonatomic) BOOL showsVerticalScrollIndicator; 是否顯?示垂直捲軸 

  4.注意點

    • 如果UIScrollView?無法滾動,可能是以下原因: 

(1)沒有設定contentSize

(2) scrollEnabled = NO

(3) 沒有接收到觸摸事件:userInteractionEnabled = NO

(4)沒有取消autolayout功能(要想scrollView滾動,必須取消autolayout) 

 

二、關於UIScrollView常見屬性的一些說明

  1.屬性使用的程式碼範例

 1 #import "MJViewController.h" 2  3 @interface MJViewController () 4 { 5     //在私人擴充中建立一個屬性 6     UIScrollView *_scrollView; 7 } 8 @end 9 10 @implementation MJViewController11 12 - (void)viewDidLoad13 {14     [super viewDidLoad];15     16     // 1.建立UIScrollView17     UIScrollView *scrollView = [[UIScrollView alloc] init];18     scrollView.frame = CGRectMake(0, 0, 250, 250); // frame中的size指UIScrollView的可視範圍19     scrollView.backgroundColor = [UIColor grayColor];20     [self.view addSubview:scrollView];21     22     // 2.建立UIImageView(圖片)23     UIImageView *imageView = [[UIImageView alloc] init];24     imageView.image = [UIImage imageNamed:@"big.jpg"];25     CGFloat imgW = imageView.image.size.width; // 圖片的寬度26     CGFloat imgH = imageView.image.size.height; // 圖片的高度27     imageView.frame = CGRectMake(0, 0, imgW, imgH);28     [scrollView addSubview:imageView];29     30     // 3.設定scrollView的屬性31     32     // 設定UIScrollView的滾動範圍(內容大小)33     scrollView.contentSize = imageView.image.size;34     35     // 隱藏水平捲軸36     scrollView.showsHorizontalScrollIndicator = NO;37     scrollView.showsVerticalScrollIndicator = NO;38     39     // 用來記錄scrollview滾動的位置40 //    scrollView.contentOffset = ;41     42     // 去掉彈簧效果43 //    scrollView.bounces = NO;44     45     // 增加額外的捲動區域(逆時針,上、左、下、右)46     // top  left  bottom  right47     scrollView.contentInset = UIEdgeInsetsMake(20, 20, 20, 20);48     49     _scrollView = scrollView;50 }51 52 - (IBAction)down:(UIButton *)sender {53     [UIView animateWithDuration:1.0 animations:^{54         //三個步驟55         CGPoint offset = _scrollView.contentOffset;56         offset.y += 150;57         _scrollView.contentOffset = offset;58         59         //_scrollView.contentOffset = CGPointMake(0, 0);60     }];61 }62 @end

  2.幾個屬性座標

  3.重要說明

(1)UIScrollView的frame與contentSize屬性的區分:UIScrollView的frame指的是這個scrollview的可視範圍(可看見的地區),contentSize是其滾動範圍。

(2)contentInset(不帶*號的一般不是結構體就是枚舉),為UIScrollView增加額外的捲動區域。(上,左,下,右)逆時針。contentInset可以使用代碼或者是視圖控制器進行設定,但兩者有區別(注意區分)。

(3)contentSize屬性只能使用代碼設定。

(4)contentOffset是個CGpoint類型的結構體,用來記錄ScrollView的滾動位置,即記錄著“框”跑到了哪裡。知道了這個屬性,就知道了其位置,可以通過設定這個屬性來控制這個“框”的移動。

(5)不允許直接修改某個對象內部結構體屬性的成員,三個步驟(先拿到值,修改之,再把修改後的值賦回去)。

(6)增加了額外地區後,contentOffset的原點在哪裡?

三、有助於理解的幾個

  模型圖:

  對比圖:

  座標圖:

iOS開發UI篇—UIScrollView控制項介紹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.