[TwistedFate]UIScrollView UIPageControl

UIScrollView

初始化及添加背景色

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
scrollView.backgroundColor = [UIColor cyanColor];

設置顯示內容區域

scrollView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight * 6);

關閉滾動條

scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;

整頁滑動

scrollView.pagingEnabled = YES;

設置回彈效果(單獨設置沒有效果,三個一起設置)

scrollView.bounces = YES;
scrollView.alwaysBounceHorizontal = YES;
scrollView.alwaysBounceVertical = YES;

設置縮放比例

scrollView.minimumZoomScale = 0.5;
scrollView.maximumZoomScale = 2;

設置代理

scrollView.delegate = self;

顯示視圖及釋放內存

[self.view addSubview:scrollView];
[scrollView release];

添加圖片

UIImageView *imageV = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
imageV.image = [UIImage imageNamed:@"1.jpg"];
imageV.tag  = 100;
// 注意:把圖片加到scrollView上
[scrollView addSubView:imageV];
[imageV release];

添加還原縮放的按鈕

UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];button.frame = CGRectMake(100, 100, 100, 100);
button.backgroundColor = [UIColor  greenColor];
[button addTarget:self action:@selector(click:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];

實現button的方法

- (void)click:(UIButton *)button{
    UIScrollView *scroll = (UIScrollView *)[self.view viewWithTag:101];
    // 重置一下縮放比例
    [scroll setZoomScale:1 animated:YES];
    // 改變一下偏移量
    [scroll setContentOffset:CGPointMake(0, kScreenHeight)animated: YES];
}

UIScrollViewDelegate的方法實現

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //NSLog(@"滑動時一直觸發");
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
    // 縮放時按中心點來縮放
        UIImageView *imageView = (UIImageView *)[self.view viewWithTag:100];
    // 改變image的中心點 始終等於self.view的中心點
    imageView.center = self.view.center;
    NSLog(@"縮放時 一直觸發");
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    // 指定要縮放的視圖 並且是scrollView的子視圖
//    UIImageView *imageView = (UIImageView *)[self.view viewWithTag:100];
//    return imageView;
    return scrollView.subviews[0];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    NSLog(@"將要開始拖動");
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSLog(@"已經結束拖拽");
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{

    NSLog(@"將要開始減速");
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    // 最終圖片停止在這個方法 ******************
    NSLog(@"已經結束減速");
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
    NSLog(@"縮放已經結束觸發");
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    // 改變偏移量的動畫結束後觸發
    NSLog(@"改變偏移量的動畫結束後觸發");
}

UIPageControl

初始化

UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, kScreenHeight - 50, kScreenWidth, 50)];
pageControl.backgroundColor = [UIColor blackColor];

設置共多少頁

pageControl.numberOfPages = 5;

設置當前顯示第幾頁

pageControl.currentPage = 2;

設置選中和未選中的顏色

pageControl.pageIndicatorTintColor = [UIColor redColor];
pageControl.currentPageIndicatorTintColor = [UIColor yellowColor];

添加單擊響應事件

[pageControl addTarget:self action:@selector(page:) forControlEvents:(UIControlEventTouchUpInside)];

添加顯示視圖

// 不能加在圖片上 否則會滑走
[self.view addSubview:pageControl];
[pageControl release];

實現單機響應事件方法

- (void)page:(UIPageControl *)page{
    NSLog(@"%ld",page.currentPage);   
}
發佈了68 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章