UIScrollVIew 滾動視圖內容總結

UIScrollVIew 滾動視圖內容總結:


- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    // 滾動視圖

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 20, 280, 440)];

    scrollView.backgroundColor = [UIColor redColor];

    

    // 設定滾動內容的範圍(CGSize)

    scrollView.contentSize = CGSizeMake(2800, 0);  // 橫縱 座標

    // 關掉邊緣的彈動搞效果

//    srollView.bounces = NO;

    // 偏移量(CGPoint)

//    srollView.contentOffset = CGPointMake(-100, -100);

    // 內容距離上 左 下 右邊緣的距離

    scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);

    // 滑動到頂部

    scrollView.scrollsToTop = YES;

    // 整屏翻動

    scrollView.pagingEnabled = YES;

    // srollView能不能滾動

    scrollView .scrollEnabled = YES; // NO:不能滾動

    // 是否顯示橫向的滾動條

    scrollView.showsHorizontalScrollIndicator = YES;

    

    

    

    // 2.將viewController設置爲ScrollView的 代理人

    scrollView.delegate = self;

    

    

    // scrollView的縮放

    // 縮放:1. 設置scrollView的縮放範圍

    scrollView.minimumZoomScale = 0.5; // 最小範圍

    scrollView.maximumZoomScale = 2;   // 最大範圍

    

    // 設置縮放時是否彈動

    scrollView.bouncesZoom = YES;

    

    [self.view addSubview:scrollView];

    [scrollView release];

    

    

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 240, 400)];

    imageView.image = [UIImage imageNamed:@"1.JPG"];

    [scrollView addSubview:imageView];

    [imageView release];

    

    UIImageView *imageView0 = [[UIImageView alloc] initWithFrame:CGRectMake(300, 20, 240, 400)];

    imageView0.image = [UIImage imageNamed:@"2.JPG"];

    [scrollView addSubview:imageView0];

    [imageView0 release];

    

    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(580, 20, 240, 400)];

    imageView1.image = [UIImage imageNamed:@"3.JPG"];

    [scrollView addSubview:imageView1];

    [imageView1 release];

    

    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(580, 20, 240, 400)];

    imageView2.image = [UIImage imageNamed:@"4.JPG"];

    [scrollView addSubview:imageView2];

    [imageView2 release];

    

    UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(860, 20, 240, 400)];

    imageView3.image = [UIImage imageNamed:@"5.JPG"];

    [scrollView addSubview:imageView3];

    [imageView3 release];

    

    UIImageView *imageView4 = [[UIImageView alloc] initWithFrame:CGRectMake(1140, 20, 240, 400)];

    imageView4.image = [UIImage imageNamed:@"6.JPG"];

    [scrollView addSubview:imageView4];

    [imageView4 release];

    

//    UIImageView *imageView5 = [[UIImageView alloc] initWithFrame:CGRectMake(1420, 20, 240, 400)];

//    

//    [scrollView addSubview:imageView5];

//    [imageView5 release];

 

    


    

    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(20, 420, 280, 62)];

//    pageControl.backgroundColor =[UIColor clearColor];

    

    // 顯示多少個點點

    pageControl.numberOfPages = 5;

    pageControl.currentPageIndicatorTintColor = [UIColor blackColor];

    

    // 給pageControl加一個響應方法

    [pageControl addTarget:self action:@selector(pageControlAction:)forControlEvents:UIControlEventValueChanged];

    

    [self.view addSubview:pageControl];

    [pageControl release];

    

    

    // 方便在其他方法中調用pageControl

    self.page = pageControl;

}


- (void)pageControlAction:(UIPageControl *)pageControl

{

    NSLog(@"%d", pageControl.currentPage);

}

//  縮放:2. 設置一個視圖 隨着scrollView放大縮小

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

{

    return [scrollView.subviews firstObject]; // 把第一個子視圖隨着scrollView縮放

}

// 3. 實現相對應的代理方法(協議方法)

- (void)scrollViewDidSroll:(UIScrollView *)scrollView

{

//    // 只要srollView滾動 就會一直觸發這個方法

//    NSLog(@"%s", __FUNCTION__);

//    // srollView 的偏移量變化

//    NSLog(@"偏移量變化: %@", NSStringFromCGPoint(scrollView.contentOffset));

//    

//    // 判斷scrollView當前的頁數

//    // 偏移量.x / srollView寬度

//    int pageNumber = scrollView.contentOffset.x / scrollView.frame.size.width;

//    NSLog(@"頁數: %d", pageNumber);

    

    // 當前scrollView滾動到當前頁數

    self.page.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width;

    

}

// 開始拖拽的時候

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

{

    NSLog(@"開始拖拽:%s", __FUNCTION__);

}

// 結束拖拽的時候

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

{

    NSLog(@"結束拖拽:%s", __FUNCTION__);

}

// 開始減速的時候

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView

{

    NSLog(@"開始減速:%s", __FUNCTION__);

}

// 結束減速的時候

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    NSLog(@"結束減速:%s", __FUNCTION__);

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章