你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS學習筆記——滾動視圖(scrollView)

iOS學習筆記——滾動視圖(scrollView)

編輯:IOS開發綜合

滾動視圖:在根視圖中添加UIScrollViewDelegate協議,聲明一些對象屬性

@interface BoViewController : UIViewController
//滾動視圖對象
@property (retain, nonatomic) UIScrollView *scrollView;
//視圖中小圓點,對應視圖的頁碼
@property (retain, nonatomic) UIPageControl *pageControl;
//動態數組對象,存儲圖片
@property (retain, nonatomic) NSMutableArray *images;
@end

在程序中導入圖片,在.m文件中的代碼實現:

- (void)viewDidLoad
{
    [super viewDidLoad];
    //初始化scrollView
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 345)];
    //初始化pageControl
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 344, 320, 36)];
    //初始化數組,存儲滾動視圖的圖片
    self.images = [NSMutableArray arrayWithObjects:[UIImage imageNamed:@"text1.png"],[UIImage imageNamed:@"text2.png"],[UIImage imageNamed:@"text3.png"],[UIImage imageNamed:@"text4.png"], nil];
    //把scrollView與pageControl添加到當前視圖中
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.pageControl];
    //設置視圖的背景顏色
    self.view.backgroundColor = [UIColor blackColor];
    //調用 setuoPage方法
    [self setupPage:nil];	
}
//改變滾動視圖的方法實現
- (void)setupPage:(id)sender
{
    //設置委托
    self.scrollView.delegate = self;
    //設置背景顏色
    self.scrollView.backgroundColor = [UIColor blackColor];
    //設置取消觸摸
    self.scrollView.canCancelContentTouches = NO;
    //設置滾動條類型
    self.scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    //是否自動裁切超出部分
    self.scrollView.clipsToBounds = YES;
    //設置是否可以縮放
    self.scrollView.scrollEnabled = YES;
    //設置是否可以進行畫面切換
    self.scrollView.pagingEnabled = YES;
    //設置在拖拽的時候是否鎖定其在水平或者垂直的方向
    self.scrollView.directionalLockEnabled = NO;
    //隱藏滾動條設置(水平、跟垂直方向)
    self.scrollView.alwaysBounceHorizontal = NO;
    self.scrollView.alwaysBounceVertical = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    //用來記錄頁數
    NSUInteger pages = 0;
    //用來記錄scrollView的x坐標
    int originX = 0;    
    for(UIImage *image in self.images)
    {
        //創建一個視圖
        UIImageView *pImageView = [[[UIImageView alloc]initWithFrame:CGRectZero]autorelease];
        //設置視圖的背景色
        pImageView.backgroundColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0];
        //設置imageView的背景圖
        [pImageView setImage:image];
        //給imageView設置區域
        CGRect rect = self.scrollView.frame;
        rect.origin.x = originX;
        rect.origin.y = 0;
        rect.size.width = self.scrollView.frame.size.width;
        rect.size.height = self.scrollView.frame.size.height;
        pImageView.frame = rect;
        //設置圖片內容的顯示模式(自適應模式)
        pImageView.contentMode = UIViewContentModeScaleAspectFill;
        //把視圖添加到當前的滾動視圖中
        [self.scrollView addSubview:pImageView];
        //下一張視圖的x坐標:offset為:self.scrollView.frame.size.width.
        originX += self.scrollView.frame.size.width;
        //記錄scrollView內imageView的個數
        pages++;
    }
    //設置頁碼控制器的響應方法
    [self.pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
    //設置總頁數
    self.pageControl.numberOfPages = pages;
    //默認當前頁為第一頁
    self.pageControl.currentPage = 0;
    //為頁碼控制器設置標簽
    self.pageControl.tag = 110;
    //設置滾動視圖的位置
    [self.scrollView setContentSize:CGSizeMake(originX, self.scrollView.bounds.size.height)];    
}
//改變頁碼的方法實現
- (void)changePage:(id)sender
{
    NSLog(@"指示器的當前索引值為:%i",self.pageControl.currentPage);
    //獲取當前視圖的頁碼
    CGRect rect = self.scrollView.frame;
    //設置視圖的橫坐標,一幅圖為320*460,橫坐標一次增加或減少320像素
    rect.origin.x = self.pageControl.currentPage * self.scrollView.frame.size.width;
    //設置視圖縱坐標為0
    rect.origin.y = 0;
    //scrollView可視區域
    [self.scrollView scrollRectToVisible:rect animated:YES];    
}
#pragma mark-----UIScrollViewDelegate---------
//實現協議UIScrollViewDelegate的方法,必須實現的
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //獲取當前視圖的寬度
    CGFloat pageWith = scrollView.frame.size.width;
    //根據scrolView的左右滑動,對pageCotrol的當前指示器進行切換(設置currentPage)
    int page = floor((scrollView.contentOffset.x - pageWith/2)/pageWith)+1;
    //切換改變頁碼,小圓點
    self.pageControl.currentPage = page;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//釋放創建的對象
- (void)dealloc
{
    [_pageControl release];
    [_scrollView release];
    [super dealloc];
}



  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved