IOS:UI系列之UISCROLLVIEW和UIPAGECONTROL

來源:互聯網
上載者:User

IOS:UI系列之UISCROLLVIEW和UIPAGECONTROL
 轉眼間,又是一天,就這樣忙忙碌碌的一天一天的過著, 不過還好,不是渾渾噩噩的,也算是小有所成,勞有所獲吧,嘿嘿!   好了,到了總結的時間啦, 下面就為大家簡單講解下我今天學習的內容吧,希望對各位都有所協助吧,同時也是對自己的一種激勵,最終實現共贏吧 嘿嘿!   首先,在上課時間我們先簡單講述了UIScrollView, 它是一個滾動視圖,繼承於UIView,他沒有自己的初始化方法,所以要用到父類的建立方法下面就為大家簡單說明下其建立過程哈: 複製代碼UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];    scrollView.backgroundColor = [UIColor redColor];    //設定內容頁的大小,內容的大小必須比frame大,才能夠滾動    //如果不設定內容頁的大小,預設和frame大小一致    [scrollView setContentSize:CGSizeMake(320, 568 * 3)];        //設定捲軸是否可見    //水平捲軸    scrollView.showsHorizontalScrollIndicator = YES;    //豎直捲軸    scrollView.showsVerticalScrollIndicator = YES;    //設定內容頁的位移量, 預設內容頁從(0, 0)點開始    scrollView.contentOffset = CGPointMake(0, 0);    //設定整頁滑動    scrollView.pagingEnabled = NO;    //設定邊界是否回彈    scrollView.bounces = YES;    //滾動到頂部    scrollView.scrollsToTop = YES;    //設定能否滾動    scrollView.scrollEnabled = YES;    //設定邊界是否回彈,只有在content的寬度或者高度 小於 frame的寬度或者高度,才有效    scrollView.alwaysBounceVertical = YES;    scrollView.alwaysBounceHorizontal = YES;        //縮放, 縮放必須結合著代理(delegate)才會生效, 指定scrollView上哪個視圖進行縮放    //設定當前的縮放比例, 預設為1.0    scrollView.zoomScale = 1.0;    //設定最大縮放比例    scrollView.maximumZoomScale = 2.0;    //設定最小縮放比例    scrollView.minimumZoomScale = 0.5;        //設定delegate    scrollView.delegate = self;        [self.view addSubview:scrollView];    [scrollView release];        imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image"]];    imageView.frame = CGRectMake(0, 0, 320, 568 * 3);    [scrollView addSubview:imageView];    [imageView release];複製代碼其中delegate代理的常用方法如下: 複製代碼- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.} //當滾動scrollView時調用- (void)scrollViewDidScroll:(UIScrollView *)scrollView{    NSLog(@"%s", __FUNCTION__);} //當發生了縮放的時候調用- (void)scrollViewDidZoom:(UIScrollView *)scrollView{    NSLog(@"%s", __FUNCTION__);} //對ScrollView中的哪個視圖縮放- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{    return imageView;}//將要開始拖拽的時候調用- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{    NSLog(@"%s", __FUNCTION__);} //將要結束拖拽的時候調用- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{    NSLog(@"%s", __FUNCTION__);} //已經結束拖拽的時候調用- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{    NSLog(@"%s", __FUNCTION__);}複製代碼感覺UIScrollView是為接下來所講的做了一個鋪墊,以便於實現之後圖片頁碼的切換和滾動 在之後我們又講了UIPageControl UIPageControl:頁碼控制器,繼承與UIControl 他和UIScrollView一樣,也沒有自己的初始化方法,同樣要使用自己父類的方法,其具體建立方法如下所示 複製代碼- (void)viewDidLoad{    [super viewDidLoad];    //UIPageControl:頁碼控制器, 繼承於UIControl    UIPageControl *pageControl = [[UIPageControl alloc] init];    pageControl.frame = CGRectMake(0, 100, 320, 30);    pageControl.backgroundColor = [UIColor blackColor];    pageControl.numberOfPages = 10;    //設定未選中的顏色    pageControl.pageIndicatorTintColor = [UIColor yellowColor];    //設定已選中的顏色    pageControl.currentPageIndicatorTintColor = [UIColor blueColor];    //設定只有一頁時隱藏    pageControl.hidesForSinglePage = NO;    //關聯方法    [pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];    [self.view addSubview:pageControl];    [pageControl release];} - (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.} - (void)changePage:(UIPageControl *)pageControl{    //頁碼是從零開始的    NSLog(@"%d", pageControl.currentPage + 1);}複製代碼下面我們來通過一個練習來進一步對這些空間作進一步的瞭解: 通過頁面的滾動和頁碼的切換來調用方法進而使效果類似於我們看電子書一樣翻頁跳轉, 下面我就來附上代碼供大家參考: 複製代碼- (void)changePage:(UIPageControl *)pageControl{    if (pageControl.currentPage == 0) {        [scrollView1 setContentOffset:CGPointMake(0, 0) animated:YES];    } else if (pageControl.currentPage == 1) {        [scrollView1 setContentOffset:CGPointMake(320, 0) animated:YES];    } else if (pageControl.currentPage == 2) {        [scrollView1 setContentOffset:CGPointMake(320 * 2, 0) animated:YES];    }} - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{    pageControl1.currentPage = scrollView.contentOffset.x / 320;} - (void)close:(UIButton *)button1{    //如果子視圖的父視圖被移除,那麼這個子視圖也會被移除    [button removeFromSuperview];    [scrollView1 removeFromSuperview];    [pageControl1 removeFromSuperview];}複製代碼其中- (void)close:(UIButton *)button1 這個方法是用來跳轉首頁的, 就像我們在開啟一款應用的時候上面都會顯示一些新功能簡介

聯繫我們

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